Apache http client or URLConnection

Amit Raz picture Amit Raz · Jan 25, 2011 · Viewed 65.7k times · Source

I need to download a web page on an android app and I am having a hard time deciding whether to use the android apache http client or java's URLConnection.

Any thoughts?

Answer

Idolon picture Idolon · Oct 12, 2011

Google has silently deprecated Apache HTTP client usage since Gingerbread: http://android-developers.blogspot.com/2011/09/androids-http-clients.html. And while they didn't mark it with deprecated annotation, they suggest you to use HttpURLConnection for new applications as: it is where we [Google] will be spending our energy going forward.

Personally I don't like that decision and would rather stick to HttpClient 4.1+, as it is faster, have fewer bugs and is updated regularly. And while you can not upgrade system library to version 4.1, you can include HttpClient jar to your Android project (as the additional benefit this would allow you to not depend on Google bug fixes and vendor updates). There is one pitfall however: to prevent possible collisions with built-in library you should rename httpclient packages using JarJar tool. Turned out someone already did this (repackaged jar and Android library projects are available for download):

http://code.google.com/p/httpclientandroidlib/

This is a repackaging of HttpClient 4.1 for Android. The version of HttpClient in the Android SDK is 4.0beta2. There have been several updates to HttpClient and some much-needed bugfixes like auth caching since the 4.0beta.

Since Google has deprecated HttpClient in favor of Java standard HttpURLConnection I created a script to convert a stock release of Apache's HttpClient into an Android library.

Changes to stock HttpClient

  • Renamed all packages org.apache.http to ch.boye.httpclientandroidlib
  • Deleted all classes dependent on org.ietf.* (SPNEGO authentication)
  • Replaced org.apache.commons.codec.binary.Base64 with android.util.Base64
  • Created a new class HttpClientAndroidLog to replace org.apache.commons.logging