This question already has an answer here:
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?
This question already has an answer here:
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?
For most things I'd say that
HttpClient
is the way to go. However there are some situations and edge cases where I'd fall back to aURLConnection
. Examples of edge cases here and hereEDIT
A similar question has been asked before: httpclient vs httpurlconnection.
I would assume thatI would findHttpUrlConnection
is somewhat faster as theHttpClient
is built on top of the standard Java libraries. HoweverHttpClient
code much quicker and easier to write and maintain. According to a comments below, the core elements ofHttpClient
have been performance optimised.If performance is a major concern your best bet is to write two clients, one using each method, then benchmark them both. If you do this, please let us know the results.
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/
in Gingerbread and later, HttpURLConnection is the way to go. consider Apache HttpClient deprecated. (also note that Android doesn't use HttpClient 4.1, mentioned in another comment.)
if you have a case where Apache HttpClient is faster, report it as a bug here: http://code.google.com/p/android/issues/list