Apache HTTP client or URLConnection [duplicate]
Asked Answered
S

3

53

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?

Stoplight answered 25/1, 2011 at 21:39 Comment(1)
2015 Update: As of Android M, org.apache.http classes are now removed from the SDK. Here is the official release note: developer.android.com/about/versions/marshmallow/…Shiri
E
28

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 a URLConnection. Examples of edge cases here and here

EDIT
A similar question has been asked before: httpclient vs httpurlconnection. I would assume that HttpUrlConnection is somewhat faster as the HttpClient is built on top of the standard Java libraries. However I would find HttpClient code much quicker and easier to write and maintain. According to a comments below, the core elements of HttpClient 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.

Episcopalism answered 25/1, 2011 at 21:49 Comment(7)
Do you have any idea as to who is faster?Stoplight
Edited my answer to add some further infoEpiscopalism
HttpClient 4.1 should be significantly faster than HttpUrlConnection. See wiki.apache.org/HttpComponents/…. Only plain HttpCore is faster than HttpClient in my testsAssembly
@oleg that's great if it's faster as it's my preferred method. Can you elaborate on why it's so much quicker?Episcopalism
@Dave. A lot of efforts have been put into optimising HttpCore (the low level transport components HttpClient is based upon). Special case has been taken to ensure than only a minimal amount of intermediate garbage is created in the course of a request execution. Please note, though, I am personally involved in development of HttpClient and therefore my opinion is likely to be biased.Assembly
@oleg Unfortunately Android will never ship with HttpClient 4.1 due to so called "compatibility requirements" (bla-bla… what about VMRuntime which was removed since API 8?). Personally I think they just didn't want to bother with keeping httpclient up to date with your development (and now Google silently decided to deprecate it), but that doesn't change the point - the only way to use HttpClient 4.1 on Android is to include it as separate Jar renaming packages to avoid class collisions with built-in version.Chlorinate
I found this blog by a member of the Android Dalvik team. It says that it is best to use the HttpURLConnection moving forward. android-developers.blogspot.com/2011/09/…Bookrack
C
45

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
Chlorinate answered 12/10, 2011 at 15:56 Comment(2)
Hello there, I am using your library right now to upload files using MultipartEntity Builder and HttpEntity. But I am having concerns if the library is still relevant for 22 and above devices.Tracheitis
@John First of all it's not my library, as I never contributed to that project. Second, this answer is 4-years old and the library seems not to be updated till the beginning of 2014 and contains vulnerabilities which were fixed in the latest versions of HttpClient (see code.google.com/p/httpclientandroidlib/issues/detail?id=19). I will update my answer shortly, until then I suggest you to either use one of these: hc.apache.org/httpcomponents-client-4.5.x/android-port.html or switch to OkHttp client: square.github.io/okhttpChlorinate
E
28

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 a URLConnection. Examples of edge cases here and here

EDIT
A similar question has been asked before: httpclient vs httpurlconnection. I would assume that HttpUrlConnection is somewhat faster as the HttpClient is built on top of the standard Java libraries. However I would find HttpClient code much quicker and easier to write and maintain. According to a comments below, the core elements of HttpClient 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.

Episcopalism answered 25/1, 2011 at 21:49 Comment(7)
Do you have any idea as to who is faster?Stoplight
Edited my answer to add some further infoEpiscopalism
HttpClient 4.1 should be significantly faster than HttpUrlConnection. See wiki.apache.org/HttpComponents/…. Only plain HttpCore is faster than HttpClient in my testsAssembly
@oleg that's great if it's faster as it's my preferred method. Can you elaborate on why it's so much quicker?Episcopalism
@Dave. A lot of efforts have been put into optimising HttpCore (the low level transport components HttpClient is based upon). Special case has been taken to ensure than only a minimal amount of intermediate garbage is created in the course of a request execution. Please note, though, I am personally involved in development of HttpClient and therefore my opinion is likely to be biased.Assembly
@oleg Unfortunately Android will never ship with HttpClient 4.1 due to so called "compatibility requirements" (bla-bla… what about VMRuntime which was removed since API 8?). Personally I think they just didn't want to bother with keeping httpclient up to date with your development (and now Google silently decided to deprecate it), but that doesn't change the point - the only way to use HttpClient 4.1 on Android is to include it as separate Jar renaming packages to avoid class collisions with built-in version.Chlorinate
I found this blog by a member of the Android Dalvik team. It says that it is best to use the HttpURLConnection moving forward. android-developers.blogspot.com/2011/09/…Bookrack
C
14

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

Caddaric answered 10/3, 2011 at 19:27 Comment(6)
Really? Why? Did you guys announce this anywhere? I would have thought it's generally agreed that Apache HttpClient is the nicer API to use.Kex
Hughes: Google itself told us last I|O the exact opposite, that we should use HttpClient and not use HttpUrlConnection. And, since last I looked, HttpURLConnection was implemented via HttpClient. This is really rather important, and so it would be nice if we could publicize this a bit more if this is indeed the direction Google wants us to be heading.Mischief
@Mischief It was announced only recently here: android-developers.blogspot.com/2011/09/…. [HttpClient] implementation is stable and they have few bugs. But the large size of this API makes it difficult for us to improve it without breaking compatibility. The Android team is not actively working on Apache HTTP Client. OMG. I remember how oleg attempted to synchronize further project development with Android team. And now they decided to deprecate HttpClient instead - that's a real shame.Chlorinate
@Idolon: HttpClient is not deprecated; they just aren't actively attempting to improve it.Mischief
if you have a case where Apache HttpClient is faster, report it as a bug here - :)Nonrecognition
HttpClient can do streaming load whereas HttpUrlClonnection normally would load the into memory unless you use setChunkedStreamingMode which hopefully the server you are connecting to supports.Swanson

© 2022 - 2024 — McMap. All rights reserved.