Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android difference between httpclient and httpurlconnection, which one to implement? [duplicate]

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?

like image 411
Fabii Avatar asked Mar 04 '12 00:03

Fabii


4 Answers

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.

like image 25
Archimedes Trajano Avatar answered Dec 01 '22 00:12

Archimedes Trajano


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.

like image 118
Kevin Avatar answered Nov 30 '22 23:11

Kevin


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

like image 37
JaydeepW Avatar answered Dec 01 '22 01:12

JaydeepW


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.

like image 24
Nacho Coloma Avatar answered Dec 01 '22 01:12

Nacho Coloma