53

The documentation says the org.apache.http.entity.mime.MultipartEntity class is deprecated. Could anybody please suggest me an alternative ?

I am using this in my code like this:

entity.addPart("params", new StringBody("{\"auth\":{\"key\":\""
            + authKey + "\"},\"template_id\":\"" + templateId + "\"}"));
entity.addPart("my_file", new FileBody(image));
httppost.setEntity(entity);
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
nullUser
  • 1,159
  • 2
  • 15
  • 26

2 Answers2

118

If you read the docs carefully, you'll notice that you should use MultipartEntityBuilder as an alternative.

For example:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();        

/* example for setting a HttpMultipartMode */
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

/* example for adding an image part */
FileBody fileBody = new FileBody(new File(image)); //image should be a String
builder.addPart("my_file", fileBody); 
//and so on

Note that there are several constructors for the FileBody class, by which you can provide mimeType, content type, etc.

After you're done with passing build instructions to the builder, you can get the built HttpEntity by invoking the MultipartEntityBuilder#build() method:

HttpEntity entity = builder.build();
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Filebody now can't accept image as String only accept file .. please update the answer to be as last version from mime .. thank you in advance – Mycoola Mar 11 '15 at 12:48
  • 1
    Can i ask you when i post image as multipartfile .. should i set header for httppost as httppost.setheader("Content-Type", "multipart/form-data;boundary=" + boundary); some thing like this ? – Mycoola Mar 11 '15 at 13:27
  • Usually it's not painful to provide headers. This info can be useful on the server-side. Ofcourse, what exact headers and when, depends on the particular thing you need to implement. If you have some specific case, which you need help with, I suggest to ask a new question and this way you will get the attention of more people – Konstantin Yovkov Mar 11 '15 at 13:33
  • 5
    Now, MultipartEntityBuilder is not longer part of Android, as API 23. How to deal with multipart/form-data in that case? – webo80 Sep 04 '15 at 10:23
  • @webo80, probably [this article](http://android-developers.blogspot.com.br/2011/09/androids-http-clients.html) can be helpful. – Konstantin Yovkov Sep 04 '15 at 10:46
  • 2
    @kocko thanks anyway, but it was not helpful for me, it only indicates some guidelines, not at much technical level – webo80 Sep 04 '15 at 12:02
  • 1
    `HttpEntity` is also deprecated now. So can't use `builder.buid()`. What is alternative? – Ankur Raiyani Dec 12 '15 at 13:40
  • @AnkurRaiyani, [this thread](http://stackoverflow.com/questions/29150184/httpentity-is-deprecated-on-android-now-whats-the-alternative) might be helpful – Konstantin Yovkov Dec 12 '15 at 15:05
  • How do you construct a [customBodyPart](https://stackoverflow.com/questions/20672604/how-can-i-get-a-custom-content-disposition-line-using-apache-httpclient/20679480#comment110764259_20679480) using `MultipartEntityBuilder`? – Lei Yang Jun 30 '20 at 01:33
  • @LeiYang, by calling the `MultipartEntityBuilder#addPart(FormBodyPart bodyPart)` method – Konstantin Yovkov Jun 30 '20 at 06:28
  • 1
    i cannot. error is **'addPart(org.apache.http.entity.mime.FormBodyPart)' is not public in 'org.apache.http.entity.mime.MultipartEntityBuilder'. Cannot be accessed from outside package** – Lei Yang Jun 30 '20 at 06:33
  • It's public (open the documentation and check it). Probably there's an issue with your project setup. – Konstantin Yovkov Jun 30 '20 at 06:42
6

I still see so many tutorials still using the deprecated APIs which is what lead me to this post. For the benefit of future visitors (until this API gets deprecated ;) )

File image = "...."; 
FileBody fileBody = new FileBody(image);
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
                         .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                         .addTextBody("params", "{....}")
                         .addPart("my_file", fileBody);
HttpEntity multiPartEntity = builder.build();

String url = "....";
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(multiPartEntity);
...
Neo
  • 4,640
  • 5
  • 39
  • 53