About Android HTTP requests with HttpURLConnection or httpclient.

Source: Internet
Author: User

Go to http://blog.csdn.net/huzgd/article/details/8712187

Android and Java application development is the only way to submit HTTP requests, and there are basically two implementations: HttpURLConnection (i.e. url.openconnection) and httpclient.

Many people on the internet think HttpClient is better, for the reason is more powerful, fewer bugs, easier to control the details. But I personally think that ordinary Java staff can choose HttpClient, Android developers should use HttpURLConnection for the following reasons:

1.HttpClient is Apache's open source implementation, and HttpURLConnection is the Android standard implementation, Android SDK although integrated httpclient, but the official support is httpurlconnection;

2.HttpUrlConnection direct support gzip compression; HttpClient is also supported, but to write code processing on its own; we tested httpurlconnection gzip compression in the case of large file sub-trunk when there is a problem, only suitable for small files, But this bug was later officially said to have been repaired;

3.HttpUrlConnection direct Support system-level connection pool, that is, the open connection will not be closed directly, for a period of time All programs can be shared; HttpClient can certainly do it, but it is not as good as the official Direct system support;

4.HttpUrlConnection directly at the system level to do the cache policy processing, speed up the repetition of requests.

More than a year ago I began to engage in the development of Android, I wrote a small program, testing httpurlconnection and httpclient two ways to connect the connection reading speed, respectively visit the homepage of Baidu and CSDN. Select both sites because one is less data and the other is larger data. The current use of the i9000 mobile phone, Android 2.2 system, Mobile Edge network, APN for Cmnet, the program starts a service every half minutes to perform a test, including connection speed and read speed, and then save the log in CSV format analysis, the result is very unexpected, httpurlconnection read csdn than httpclient several times faster , the exact number has not been remembered. Of course, the network system environment is not the same now, I would like to run again now, but the time is long, this program has gone; just found a bit on the hard disk, the program and code did not find, had to forget. Later analysis, originally the difference should not have so big, estimate is gzip compression credit, but the connection speed on HttpURLConnection is also a little faster.

Google itself is also recommended to use HttpURLConnection, it has been a lot of optimization, this from the Android Help document can be seen:

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

Android Development Blog also stressed that 2.2 after the HttpURLConnection (this requires FQ):

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

In short, in Android development, although httpclient better support a lot of details of the control (such as agents, cookies, authentication, compression, connection pool), but correspondingly higher requirements for the developer, the code is more complex, it is difficult for ordinary developers to manage it well, the official support is also getting less While httpurlconnection the bulk of the work, shielding the unwanted details, which is more appropriate for the developer to call directly, and the official support and optimization of it will be more and more good. Since we are doing Android application development, naturally follow the official Android guidelines, choose HttpURLConnection.

About Android HTTP requests with HttpURLConnection or httpclient.

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.