org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow)

I'm upgrading an app to API 23 where org.apache.http is deprecated.

My current (deprecated) code looks like this:

HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());

I've found some suggestions to how this should be done using HttpURLConnection, but they are all much more complex then the current solution (which cannot be used anymore). I'm talking about many lines of code for executing the same functionality as the above.

Examples are: this page and this page

Does anyone have a good solid shorter solution for that?


If you change your compileSdkVersion to 21, your app will compile cleanly. That being said, there are reasons why Google is backing away from the built-in HttpClient implementation, so you probably should pursue some other library. That "some other library" could be:

  • the built-in classic Java HttpUrlConnection, though as you have found, its API leaves something to be desired
  • Apache's independent packaging of HttpClient for Android
  • OkHttp (my recommendation)
  • AndroidAsync

In particular, OkHttp seems to have a pretty good API for posting a file and posting a multipart form, which should be similar to what your HttpClient code is doing.

Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android.

Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued.

Those users who want to continue using Apache HttpClient on Android are advised to consider

Apache HttpClient 4.3 port for Android when targeting Android API 22 and older

dependencies {
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: ''

Apache HttpClient packages for Android maintained by Marek Sebera when targeting Android API 23 and newer

dependencies {
    compile group: '' , name: 'httpclient', version: ''

taken from Apache Official Website : Apache HttpClient for Android

NOTE: You do not have to use useLibrary 'org.apache.http.legacy' statement, which was introduced for projects that didn't migrate from Android provided HttpClient classes. For further explanation.

The best replacement for HTTPClient is using Volley. It is much easier to use, handles request queues and caches you requests. It is fully compatible with almost all API levels down to API 4.

See the Android documentation on how to do it.

Need Your Help

Kohana V3 return query result as object

php frameworks kohana kohana-3

In Kohana V3 is it possible to return result set as an array() or any method exists?

While loops list index out of range error. Strip off strings with double slases


I was wondering if could help me debug this code. I'm curious as to why I'm getting a list index out of range error. I'm trying to add up all the items in the list and using the number as the index...