"Official blog" Android ' s HTTP clients

Source: Internet
Author: User

Android ' s HTTP clients

[This post was by Jesse Wilson from the Dalvik team.-tim Bray]


Most network-connected Android apps would use HTTP to send and receive data. Android includes-http clients:httpurlconnection and Apache http Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling.

Apache HTTP Client

Defaulthttpclient and its sibling androidhttpclient is extensible HTTP clients suitable for web browsers. They has large and flexible APIs. Their implementation is stable and they has few bugs.

But the large size of this API makes it difficult for us to improve it without breaking compatibility. The Android team is not a actively working on Apache HTTP Client.

HttpURLConnection

HttpURLConnection is a general-purpose, lightweight HTTP client suitable for most applications. This class had humble beginnings, but its focused API had made it easy for us to improve steadily.

Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling on close() a readable inputstream could poison the connection pool. Work around the disabling connection pooling:

Private voidDisableconnectionreuseifnecessary() {
HTTP connection reuse which was buggy Pre-froyo
If (Integer.parseint (build. Version. Sdk < buildversion_codes. {
        Span class= "Typ" >system. ( "http.keepalive" , "false" );
    }
}

In gingerbread, we added transparent response compression. HttpURLConnection'll automatically add this header to outgoing requests, and handle the corresponding response:

Accept-Encoding: gzip

Take advantage of the configuring your Web server to compress responses for clients the can support it. If response compression is problematic, the class documentation shows how to disable it.

Since HTTP ' s Content-Length header returns the compressed size, it is a error to use Getcontentlength () to size buffers for the UN Compressed data. Instead, read bytes from the response until Inputstream.read () returns-1.

We also made several improvements to HTTPS in gingerbread. Httpsurlconnection attempts to connect with Server Name indication (SNI) which allows multiple HTTPS hosts to share an IP Address. It also enables compression and session tickets. Should the connection fail, it is automatically retried without these features. This makes httpsurlconnection efficient if connecting to up-to-date servers, without breaking compatibility with older O Nes.

In the Ice Cream Sandwich, we are adding a response cache. With the cache installed, HTTP requests is satisfied in one of the three ways:

    • Fully cached responses is served directly from local storage. Because No network connection needs to be made such responses is available immediately.

    • Conditionally cached responses must has their freshness validated by the webserver. The client sends a request like "Give me/foo.png if it changed since yesterday" and the server replies with either the UP Dated content or a 304 Not Modified status. If the content is unchanged it won't be downloaded!

    • Uncached responses is served from the Web. These responses would get stored in the response cache for later.

Use the reflection to enable HTTP response caching on devices. This sample code would turn on the response cache on Ice Cream Sandwich without affecting earlier releases:

Private voidEnablehttpresponsecache() {
Try {
LongHttpcachesize= 10 * 1024 * 1024; Ten MiB
FileHttpcachedir= New File(Getcachedir(), "http");
Class.Forname("Android.net.http.HttpResponseCache")
.GetMethod("Install", File.class, long.)
            .invoke (null, httpcachedir< Span class= "pun" >, Httpcachesize     } catch (exception httpresponsecachenotavailable {
    }< Span class= "PLN" >
}

You should also configure your WEB server to set cache headers on its HTTP responses.

Which client is the best?

Apache HTTP Client has fewer bugs on Eclair and Froyo. It's the best choice for these releases.

For gingerbread and better, httpurlconnection are 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 we'll be spending going forward.

"Official blog" Android ' s HTTP clients

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.