It is better to use httpurlconnection or httpclient for Android HTTP requests

Source: Internet
Author: User

HTTP requests must be submitted for Android and Java application development. Basically, there are two implementation methods: httpurlconnection (URL. openconnection) and httpclient.

Many people on the internet think that httpclient is better, because it has stronger functions, fewer bugs, and easier to control details. However, I personally think that httpclient can be used by common Java users, while Android Developers should use httpurlconnection for the following reasons:

1. httpclient is an open-source implementation of Apache, while httpurlconnection is an android standard implementation. Although the android SDK integrates httpclient, httpurlconnection is officially supported;

2. httpurlconnection directly supports gzip compression; httpclient also supports, but you need to write your own code for processing. We previously tested that the gzip compression of httpurlconnection has problems when transferring large file packages to trunk, which is only suitable for small files, however, the bug was officially fixed;

3. httpurlconnection directly supports the system-level connection pool, that is, the opened connection will not be closed directly, and all programs can be shared within a period of time. The httpclient can also do the same, but it is not as good as the underlying support of the official direct system;

4. httpurlconnection directly processes cache policies at the system level to speed up repeated requests.

When I started Android development more than a year ago, I wrote a small program to test the connection reading speed of httpurlconnection and httpclient, respectively accessing the homepage of Baidu and csdn. Select these two websites because one is less data and the other is larger data. Currently, i9000 mobile phones, Android 2.2 systems, mobile EDGE networks, and APN are cmnet. The program starts a service and performs a test every half minute, including the connection speed and read speed, then, the log is saved and analyzed in CSV format. The result is very unexpected. httpurlconnection reads csdn several times faster than httpclient, and the specific number cannot be remembered. Of course, the network system environment is different now. I wanted to run it again now, but for a long time, this program is gone. I just looked for it on the hard disk and did not find the program and code, forget it. Later, I analyzed that the difference between the two should not be so big. It is estimated that gzip compression will be used, but httpurlconnection will be faster in terms of connection speed.

Google also recommends httpurlconnection, which has been greatly optimized. This can be seen from the help document of Android:

Http://developer.android.com/reference/java/net/HttpURLConnection.html

The android development blog also stressed that httpurlconnection should be used for Android 2.2 and later versions (this requires a wall flip ):

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

In short, in Android development, although httpclient better supports control of many details (such as proxy, Cookie, authentication, compression, and connection pool), it has higher requirements for developers, code writing is more complex, and it is difficult for common developers to control it well, with fewer official support. httpurlconnection encapsulates most of the work, it shields unnecessary details and is more suitable for developers to directly call it. In addition, official support and Optimization for it will become better and better. Since we are developing Android applications, we naturally need to follow the official Android guidelines and choose httpurlconnection.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.