9

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");
request.setEntity(fileEntity);
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?

Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110
Ambran
  • 2,367
  • 4
  • 31
  • 46
  • 2
    See https://github.com/square/okhttp/wiki/Recipes#posting-a-file and https://github.com/square/okhttp/wiki/Recipes#posting-a-multipart-request for OkHttp recipes of potential relevance. – CommonsWare Oct 05 '15 at 13:31
  • @CommonsWare, thanks for the link. I've been reading about OkHttp, and decided to use it in my app. It takes care of many issues for me and is quite easy to use. Thanks for the tip. Write this as an answer and i'll mark you. – Ambran Oct 05 '15 at 14:23
  • Another option for you to refer if you want to use Volley http://stackoverflow.com/questions/32240177/working-post-multipart-request-with-volley-and-without-httpentity – BNK Oct 06 '15 at 01:30

3 Answers3

9

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @Commonsware: Can we use Volley to replace HttpClient? – ARK Jan 28 '16 at 21:09
  • @akshayrajkore: Yes, Volley would be another option. Compared to the ones that I list, I am not as much of a fan of it, simply because Google is not supporting it all that well (e.g., no actual JAR or AAR). – CommonsWare Jan 28 '16 at 21:25
  • @CommonsWare: From what I was able to understand, from your book and other sources is that volley also uses the deprecated apache client library. Why would we still use it knowing this? – ARK Jan 28 '16 at 21:41
  • @akshayrajkore: "other sources is that volley also uses the deprecated apache client library" -- only on Android 2.2 and older. On anything newer, it uses `HttpUrlConnection`. – CommonsWare Jan 28 '16 at 21:44
  • @CommonsWare: what are your thoughts on retrofit? – ARK Jan 28 '16 at 22:06
  • 1
    @akshayrajkore: Retrofit is not an equivalent to anything discussed above, as Retrofit is only for calling Web services. Under the covers, Retrofit will use OkHttp. – CommonsWare Jan 28 '16 at 22:30
4

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: '4.3.5.1'
}

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

dependencies {
    compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}

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.

Vipul Asri
  • 8,903
  • 3
  • 46
  • 71
  • Thanks for that, I can see that there is no real reason to use much time on using HttpURLConnection directly when good alternatives exist. I've decided to use the OkHttp library (which itself uses HttpURLConnection but surely more efficiently then I would do). If it's good enough for Twitter and FB then it's probably good enough for me. – Ambran Oct 05 '15 at 14:27
  • 1
    Or you can add this to your `build.gradle`: `android { useLibrary 'org.apache.http.legacy' }`. See [Android 6.0 Changes](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#boringSSL). – Sufian Jun 30 '16 at 09:02
0

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.

SoroushA
  • 2,043
  • 1
  • 13
  • 29