Setting up multipart messages using the DefaultHttpClient

Quickly share your Android Code Snippets here...

Setting up multipart messages using the DefaultHttpClient

Postby coding_android » Sat Oct 04, 2008 4:14 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import org.apache.http.entity.mime.MultipartEntity;
  2. import org.apache.http.entity.mime.content.FileBody;
  3. import org.apache.http.entity.mime.content.StringBody;
  4. import java.io.File;
  5. import java.io.UnsupportedEncodingException;
  6.  
  7. ...
  8.  
  9. MultipartEntity multipart = new MultipartEntity();
  10. File file = new File("/filepath");  // File with some location (filepath)
  11. Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
  12. FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
  13. multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file
  14. StringBody stringB;  // Now lets add some extra information in a StringBody
  15.         try {
  16.                 stringB = new StringBody("I am the caption of the file",chars);  // Adding the content to the StringBody and setting up the encoding
  17.                 multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
  18.                 } catch (UnsupportedEncodingException e) {
  19.                         // TODO Auto-generated catch block
  20.                         e.printStackTrace();
  21.                 }
  22.                
  23. HttpPost post = new HttpPost(url); // Setting up a HTTP Post method with the target url
  24. post.setEntity(multiPartEntity); // Setting the multipart Entity to the post method
  25. HttpResponse resp = client.execute(post);  // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
  26.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


In order to use this snippet you have to import the following libraries:

http://james.apache.org/mime4j/index.html - Mime4j
http://hc.apache.org/httpcomponents-cli ... index.html - HttpMime
http://commons.apache.org/io/ - Commons IO

SDK Version compatible: 0.9, 1.0
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Top

Postby thomasmckay » Fri Nov 21, 2008 7:13 pm

This is a very useful start for me but I'm stuck still.

Given a byte[] how would I include it in the MultipartEntity?
thomasmckay
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 21, 2008 7:10 pm

Postby thomasmckay » Fri Nov 21, 2008 9:27 pm

ByteArrayInputStream byteStream = new ByteArrayInputStream(label);
InputStreamBody is = new InputStreamBody(byteStream, "myimage");
entity.addPart("image", is);

Something along those lines?
thomasmckay
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 21, 2008 7:10 pm

Postby thomasmckay » Mon Nov 24, 2008 11:23 pm

Though it took me far too long to solve this, here's what I found.

First, the MultipartEntity works fine for StringBody but fails with any streaming types. When a streaming type body is added (like InputStreamBody) the getContentLength() method will return -1. This, in my case, causes the server side code to choke. My solution was to create a new ByteBody for the Android-created jpeg.

I hope this is helpful to someone flailing away like I was.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public class ByteBody extends AbstractBody implements BinaryBody, ContentBody {
  3.  
  4.  
  5.  
  6.         private final byte[] mContent;
  7.  
  8.    
  9.  
  10.         public ByteBody(final byte[] content) {
  11.  
  12.             super();
  13.  
  14.             if (content == null) {
  15.  
  16.                 throw new IllegalArgumentException("File may not be null");
  17.  
  18.             }
  19.  
  20.             this.mContent = content;
  21.  
  22.         }
  23.  
  24.    
  25.  
  26.         public InputStream getInputStream() throws IOException {
  27.  
  28.             return(new ByteArrayInputStream(mContent));
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         public void writeTo(final OutputStream out, int mode) throws IOException {
  35.  
  36.             if (out == null) {
  37.  
  38.                 throw new IllegalArgumentException("Output stream may not be null");
  39.  
  40.             }
  41.  
  42.             out.write(mContent, 0, mContent.length);
  43.  
  44.         }
  45.  
  46.  
  47.  
  48.         public String getTransferEncoding() {
  49.  
  50.             return MIME.ENC_BINARY;
  51.  
  52.         }
  53.  
  54.  
  55.  
  56.         public String getCharset() {
  57.  
  58.             return null;
  59.  
  60.         }
  61.  
  62.  
  63.  
  64.         public String getMimeType() {
  65.  
  66.             return "application/octet-stream";
  67.  
  68.         }
  69.  
  70.    
  71.  
  72.         public Map<?, ?> getContentTypeParameters() {
  73.  
  74.             return Collections.EMPTY_MAP;
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         public String getMediaType() {
  81.  
  82.             return "application";
  83.  
  84.         }
  85.  
  86.  
  87.  
  88.         public String getSubType() {
  89.  
  90.             return "octet-stream";
  91.  
  92.         }
  93.  
  94.  
  95.  
  96.         public long getContentLength() {
  97.  
  98.             return this.mContent.length;
  99.  
  100.         }
  101.  
  102.  
  103.  
  104.         public String getFilename() {
  105.  
  106.             return("bytearray");
  107.  
  108.         }
  109.  
  110.     }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
thomasmckay
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 21, 2008 7:10 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 7 guests