17

I am looking to create a login form for an Android application. I want to use a post method to send information to the server side where it is handled by a PHP file; which in turn validates the parameters and sends back a response.

I've looked through implementations using HttpClient and URLConnection, and they are very similar. Which is more efficient for use within an Android app?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabii
  • 3,820
  • 14
  • 51
  • 92
  • 2
    Check out the official android blog [here](http://android-developers.blogspot.co.nz/2011/09/androids-http-clients.html), worth to read. – yorkw Mar 04 '12 at 01:15
  • Possible duplicate of http://stackoverflow.com/q/4799151/741249 – THelper Dec 19 '12 at 11:25

6 Answers6

20

I believe in this case it's up to whichever API you find more natural. Generally, HTTPClient is more efficient inside a server side application (or maybe batch application), because it allows you to specify a multithreaded connection pool, with a max number of total connections, and a max per host connection count (which ensures concurrent connections to the same host don't get serialized (a problem with HttpUrlConnection)). But in an android app, you'll probably only be making a single connection at a time, so this doesn't matter.

Kevin
  • 24,871
  • 19
  • 102
  • 158
19

I have done a bit of research over this.

I have been using Apache HttpClient for a long time on Android. It looked like a natural choice to me and I thought that it would be improved over time.

On the other hand, while I was developing for legacy BlackBerry OS, I have been using HttpUrlConnection.

It was clearly evident to me that the performance of BlackBerry was better than Android in context of networking.

HttpClient is a fully functional, but buggy, class that provides a huge set of APIs/methods. It can be used to create a fully functional web browser for Android. But it has some issues on older versions of Android and it’s not actively being contributed to by Google.

Whereas HttpUrlConnection has a pretty useful API that is just useful to develop a networking client application. It has improved response caching and improved compression technique on Android 2.3 and above. It is recommenced when you are building a networking client application.

"Apache HTTPClient has fewer bugs on Android 2.1 (Eclair) and Android 2.2 (Froyo). It is the best choice for these releases.

For Android 2.3 (Gingerbread) and better, HttpURLConnection is the best choice. Its a simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where Google will be spending its energy going forward."

Reference for details

Android’s HTTP Clients

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JaydeepW
  • 3,237
  • 1
  • 25
  • 27
10

According to the Android team you should be using HttpURLConnection on Android 2.3 (Gingerbread) and better, since that is where they will put new development effort.

Android’s HTTP Clients

These days I have found OkHttp by Square, that includes SPDY support and automatic retries.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nacho Coloma
  • 7,070
  • 2
  • 40
  • 43
1

I would generally recommend URLConnection because it can get updated with the JDK. In one case we had a call that used an older version of HTTP Client which didn't support TLS v1.2.

However, I wouldn't use URLConnection directly. I would generally use a higher level API like a JAX-RS client or the wsimport clients to connect to another site.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
0

Though the tag is specifically for Android, in general HttpURLConnection is also the better choice when it comes to Java EE applications as it will utilize the HTTP stack that comes with the application server which includes configuration of HTTPS certificates in the application server level rather than the code.

It will also allow you to obtain the latest version of SSLs provided by the application server stack rather than being stuck with an old version of httpclient that may not work with TLS 1.2

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
0

If you are not trying to send and receive large files, I'd suggest HttpClient.

It is much easier to get started and use, and there are many more working examples available on the Internet.

NOTE: HttpClient is different from HTTPClient (note case) which is another vendor's implementation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RightHandedMonkey
  • 1,718
  • 20
  • 25