Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLConnection or HTTPClient: Which offers better functionality and more efficiency?

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 814
Fabii Avatar asked Mar 04 '12 00:03

Fabii


People also ask

Which is the best HttpClient for Java?

Apache HttpClient The Apache HTTP client was the first widely adopted open source client to be released, arriving in its original form in 2002. Now on its 5th major version, it's probably still the most commonly used client outside of Java's core libraries.

Do we need to close HttpClient connection?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

Should I disconnect HttpURLConnection?

If using HttpURLConnection , do not disconnect your connections after you read their response, consider increasing the socket pool size, and be careful of related problems.

Is HttpClient available in Java 8?

Is there any way to use it in java 8? No, because the jdk. incubator. http module has been added since Java 9.


4 Answers

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 186
Kevin Avatar answered Oct 06 '22 01:10

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 20
JaydeepW Avatar answered Oct 05 '22 23:10

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 40
Nacho Coloma Avatar answered Oct 05 '22 23:10

Nacho Coloma


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 32
Archimedes Trajano Avatar answered Oct 05 '22 23:10

Archimedes Trajano