Android’s HTTP Clients
September 29th, 2011 | Published in Google Android | 1 Comment
[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 includes 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 sibling AndroidHttpClient 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 for us to improve steadily.
Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling close()
on a readable InputStream could 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)
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 configuring your Web server to compress responses for clients that 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 an error to use getContentLength() to size buffers for the uncompressed 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 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 or a
304 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 should 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 should use HttpURLConnection; it is where we will be spending our energy going forward.
October 28th, 2011 at 4:13 pm (#)
Hi Jesse,
Great post. Thanks. I actually stumbled upon this after reading this article: http://www.fiercedeveloper.com/story/google-we-must-incentivize-developers-build-less-bandwidth-intensive-apps/2011-08-12?utm_medium=nl&utm_source=internal
Clearly data usage is becoming a big problem. Consumers are already feeling this pain as carriers move away from unlimited plans. I’m glad to see that you guys are being proactive about this issue.
I had the same thought you guys had. Why not check the request before sending it. If compression is not set, go ahead and set it. If the server sends a compressed response, then decompress it before returning the response to the client. This could save a ton of bandwidth without any application having to change.
I’m a bit saddened that your focus is only on HttpURLConnection though. When I started Android development a while back and did research on HttpURLConnection vs. HTTPClient, most everything I read recommended HTTPClient. That leads me to believe that the majority of apps are likely using HTTPClient over HttpURLConnection. Pure speculation of course, but that’s my assumption. Anyway, I don’t see that this couldn’t be done without breaking API’s. HTTPClient is pretty simple. You call execute, passing the request, and get the response back. I’m not looking at the source code of course, but on the surface it looks easy enough.
Also, what’s the reason that these type of changes can’t go into point updates? Maybe the HttpURLConnection change was breaking since it affected getContentLength()? But if the same could be done to HTTPClient with no breaks, you’d think it could be rolled out to 2.x and 2.3.x which are the vast majority of the install base right now.
Thanks,
Warren