Select an Http client on Android)

Source: Internet
Author: User

Most Android apps with network connections Use HTTP to send and receive data. Android contains two types of Http client classes: HttpURLConnection and Apache HttpClient. Both support HTTPS, streaming upload and download, configuration timeout, IPv6, and connection pool.

Apache Http Client

DefaultHttpClient and Its sibling AndroidHttpClient are eXtensible Http clients for web browsers. They have a large number of flexible APIs, with stable implementations and few bugs. However, its huge API makes it difficult for us to improve without compromising its compatibility. The Android team is currently inactive and working with Apache HttpClient.

HttpURLConnection

HttpURLConnection is a lightweight Http client that can be used for various applications. This class is relatively simple at first, but the APIS it focuses on make it easy to improve smoothly. Before Froyo (2.2), HttpURLConnection had some frustrating bugs. In particular, calling close () on a readable InputStream will impede the connection pool. To solve this bug, you can only turn off the connection pool.

 

1 private void disableConnectionReuseIfNecessary (){
2 // HTTP connection reuse which was buggy pre-froyo
3 if (Integer. parseInt (Build. VERSION. SDK) <Build. VERSION_CODES.FROYO ){
4 System. setProperty ("http. keepAlive", "false ");
5}
6}

 

In Gingerbread (2.3), we have added transparent response compression. HttpURLConnection automatically adds the "Accept-Encoding: gzip" header field to the request and processes the corresponding response. By changing your Web server configuration, you can return compressed data to supported clients. If the compression fails, the class document will provide a method to disable it.

Because the HTTP Content-Length header field returns the compressed size, it is wrong to use getContentLength () to allocate the buffer size after decompression. The Bytes should be read from response until the value of InputStream. read () is-1.

We also made some improvements to HTTPS on Gingerbread. HttpsURLConnection tries to connect with Server Name Indication (SNI). SNI allows multiple HTTPS hosts to share the same IP address. HttpsURLConnection can also use the compression and session ticket features ., Once the connection fails, it automatically does not use these features to retry. This allows HttpsURLConnection to effectively connect to the latest server without compromising compatibility with old servers.

At Ice Cream Sandwich (4.0), we added the response cache. After the cache is installed, HTTP requests are processed in one of the following three ways:

The full cache response will be obtained directly from the local storage. Because no network connection is required, such response can be obtained immediately.

Response with conditional cache must verify the validity of the cache on the Web server. The client sends a request, for example, "If/foo.png has changed since yesterday, it will give me a new image". The server's response is either the updated content, or the 304 status code is not modified. If the content does not change, you do not need to download it.

Response without cache will be obtained from the server. After the response is obtained, it will be stored in the cache for future use.

Use the reflection mechanism to use the Https response cache function. The following sample code will enable the response cache function on Ice Cream Sandwich without affecting the previous version:

 

1 private void enableHttpResponseCache (){
2 try {
3 long httpCacheSize = 10*1024*1024; // 10 MiB
4 File httpCacheDir = new File (getCacheDir (), "http ");
5 Class. forName ("android.net. http. HttpResponseCache ")
6. getMethod ("install", File. class, long. class)
7. invoke (null, httpCacheDir, httpCacheSize );
8} catch (Exception httpResponseCacheNotAvailable ){
9}
10}

 

Of course, you also need to change the configuration of your Web server and set the cache header field in its Http response.

Which http client is the best?

Apache HTTP client has fewer bugs on Eclair (2.1) and Froyo (2.2), which is the best choice for these system versions.

From Gingerbread (2.3), HttpURLConnection will be the best choice. Its API is simple and small, and it is very suitable for Android. Transparent compression and response cache reduce network traffic, improve network speed, and reduce power consumption. The new application should use HttpURLConnection, which is a place we will spend time exploring.


From: http://www.cnblogs.com/mudoot/archive/2011/11/30/android_http_clients.html

========================================================== ======================================
Copyright is jointly maintained. If you need to reprint the copyright, please indicate the reprinted address.

Original address of the current blog: wisekingokok.cnblogs.com
========================================================== ======================================

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.