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

后端 未结 6 1980
臣服心动
臣服心动 2020-11-27 18:45

I 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 handle by a PHP file; which in tu

相关标签:
6条回答
  • 2020-11-27 19:05

    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 JAX-RS Client or the wsimport Clients to connect to another site.

    0 讨论(0)
  • 2020-11-27 19:06

    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

    0 讨论(0)
  • 2020-11-27 19:08

    I have done a bit of research over this, I have been using Apache HttpClient for a long time in Android. It looked a natural choice to me and thought that it will be improved over time.

    On the other side while I was developing for legacy BlackBerryOS, I have been using HttpUrlConnection.

    It was clearly evident to me that the performance of BB 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 WebBrowser for Android. But it has some issues on older version of Android and its 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 Eclair and Froyo. It is the best choice for these releases.

    For Gingerbread and better, HttpURLConnection is the best choice. Its 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."

    Refer for details

    http://android-developers.blogspot.in/2011/09/androids-http-clients.html

    0 讨论(0)
  • 2020-11-27 19:14

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

    http://android-developers.blogspot.de/2011/09/androids-http-clients.html

    Edit: These days I have found okhttp by Square, that includes SPDY support and automatic retries: https://github.com/square/okhttp

    0 讨论(0)
  • 2020-11-27 19:19

    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 much more working examples available on the internet.

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

    0 讨论(0)
  • 2020-11-27 19:24

    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.

    0 讨论(0)
提交回复
热议问题