1

I want to send some text and an image to server. I am using MultipartEntityBuilder for this purpose. But as of API 22 HttpEntity is depreciated. So, how should I add my multipart to the HttpURLConnection?

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(MIME.UTF8_CHARSET);

multipartEntityBuilder.addTextBody("fname", fname, ContentType.create("text/plain", MIME.UTF8_CHARSET));
                    .
                    .
                    .
multipartEntityBuilder.addTextBody("admin_id", adminDataListener.getAdminId(),
                            ContentType.create("text/plain", MIME.UTF8_CHARSET));

if (imageAttached){
multipartEntityBuilder.addTextBody("existing_image", adminDataListener.getAdminId(),
ContentType.create("text/plain", MIME.UTF8_CHARSET));
                        multipartEntityBuilder.addBinaryBody("image1 ", newImageFile, ContentType.MULTIPART_FORM_DATA,
                                newImageFile.getName());
} else {
        multipartEntityBuilder.addTextBody("existing_image", adminDataListener.getAdminImage(),
        ContentType.create("text/plain", MIME.UTF8_CHARSET));
        }
Atif Farrukh
  • 2,219
  • 2
  • 25
  • 47
  • 1
    Try okhttp from square. https://github.com/square/okhttp/wiki/Recipes – Raghunandan Jul 16 '15 at 06:01
  • @Raghunandan I will look into it, thanks. – Atif Farrukh Jul 16 '15 at 11:18
  • Facing the same problem. Sorry, but "use okhttp" is not suitable for me, I'm already using Ion, and I don't want to use two similar libraries. I suppose must will be a form to use HTTPURLConnection (as Google recommends) with multipart/form-data – webo80 Sep 04 '15 at 10:16
  • In API 22 is deprecated, but in 23 it's gone :( – webo80 Sep 04 '15 at 10:17
  • @webo80 yes they had removed the API but you can use it by adding `useLibrary 'org.apache.http.legacy'` in you `build.gradle`. For details check [Apache HTTP Client Removal](https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client) – Atif Farrukh Sep 06 '15 at 05:57
  • I want to think there is the possibility to do it with only vanilla Android, not with 3rd party libraries. Thanks anyway @AtifFarrukh – webo80 Sep 07 '15 at 07:20
  • @webo80 this is the same library that Android removed it in API-23. If I am not wrong. – Atif Farrukh Sep 07 '15 at 09:36

1 Answers1

-2

You have to define a boundary and use it building the multipart payload.

String boundary = "-----stackoverflow--------" + System.currentTimeMillis();

multipartEntityBuilder.setBoundary(boundary);

Then you should tell the used boundary to the server with:

conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

Finally build a HttpEntry and let it write its content to the outputstream

HttpEntity entity = multipartEntityBuilder.build();
entity.writeTo(conn.getOutputStream());

Getting responsecode and returned page in the usual way.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • 2
    But the HttpEntity is depreciated in API 22. That's the problem I am facing. – Atif Farrukh Jul 16 '15 at 11:07
  • Sorry i oversaw that you said that. I assumed you said HttpClient or so. Readed to quick... If MultipartEntityBuilder is not deprecated than a strange situation occurs indeed. – greenapps Jul 16 '15 at 11:17