Android's HTTP clients (Android HTTP client interface)

Source: Internet
Author: User
Document directory
  • Apache HTTP client
  • Httpurlconnection
  • Which client is best?
Android's HTTP clients

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

[This post is by Jesse Wilson from the Dalvik team.-Tim Bray]


Most network-connected Android apps will use HTTP to send and receive data. android ipvdes two 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 siblingandroidhttpclient are eXtensible
HTTP clients suitable for Web browsers. They have large and flexible APIs. Their implementation is stable and they have 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 actively working on Apache HTTP client.

Httpurlconnection

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

Prior to Froyo, httpurlconnection had some frustrating bugs. In particle, callingclose()On a readable inputstream cocould
Poison the connection pool. work around this by disabling Connection pooling:

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

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

Accept-Encoding: gzip

Take advantage of this by grouping your web server to compress responses for clients that can support it. If response compression is problematic, theclass documentation
Shows how to disable it.

Since HTTP'sContent-LengthHeader returns the compressed size, it is an error to usegetcontentlength () to size buffers for the uncompressed
Data. Instead, read bytes from the response untilinputstream. Read () returns-1.

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

In ice cream sandwich, we are adding a response cache. With the cache installed, HTTP requests will be satisfied in one of three ways:

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

  • Conditionally cached responses must have 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 updated content or304 Not Modified
    Status. if the content is unchanged it will not be downloaded!

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

Use reflection to enable HTTP Response caching on devices that support it. This sample code will turn on the Response cache on ice cream sandwich without affecting earlier releases:

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

You shoshould also configure your web server to set cache headers on its HTTP responses.

Which client is best?

Apache HTTP client 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 shocould usehttpurlconnection;
It is where we will be spending our energy going forward.

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.