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?
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.
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.
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 there any way to use it in java 8? No, because the jdk. incubator. http module has been added since Java 9.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With