`
ericbaner
  • 浏览: 173356 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android HTTP Clients使用选择介绍

 
阅读更多

翻译稿:

 

大多数需要网络连接的Android应用会使用HTTP协议发送和接收数据。Android包含两种Http客户端类, HttpURLConnection和Apache的HttpClient。这两者都支持HTTPS,streaming 上传与下载,配置超时时间,IPv6, 以及连接池。

 

Apache Http Client

 

DefaultHttpClient 和它的兄弟类AndroidHttpClient是可扩展的Http客户端,适用于Web浏览器。它们有大量的灵活的API,其实现是稳定的,极少有bug. 但是, 其巨大的API数量导致我们难以改进它们而不破坏兼容性。 Android团队现在已不活跃于在Apache HttpClient上的工作了。

 

HttpURLConnection

 

HttpURLConnection是一个适用于多数应用的通用的,轻量级的Http客户端。该类起初较粗糙,但其关注的API让我们容易稳固地改进它。 在Froyo(2.2)之前,HttpURLConnection有一些令人沮丧的的bug。特别是,在一个可读的InputStream上调用close()可阻碍连接池。绕过这个bug只能关掉连接池。

 

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");
    }
}

 

在Gingerbread(2.3),我们增加了透明的应答压缩。HttpURLConnection将自动地将“Accept-Encoding: gzip” 头字段添加进上行的请求,并处理相应的应答。 通过配置你的Web服务器,对支持的客户端返回压缩后的应用数据。如果应用压缩是有问题的,可参见类文档如何关掉它。

 

因为HTTP的Content-Length头字段返回的是压缩后的大小, 使用getContentLength()来分配解压缩后数据的buffer 大小是错误的。 应该从应答中读字节直到InputStream.read()返回-1。

 

我们还在Gingerbread上对HTTPS作出了一些改进。HttpsURLConnection尝试以Server Name Indication(SNI)连接, SNI允许多个HTTPS host共享一个IP地址。HttpsURLConnection还具备压缩和会话ticket功能。没有这些功能,一旦连接失败,它会自动重试。这使得HttpsURLConnection有效地连接最新的服务器,同时不破坏对老旧服务器的兼容性。

 

在Ice Cream Sandwich(2.4 or 4.0),我们增加了应答cache。配备了cache后,HTTP 请求以以下三种方式之一处理:

 

1)全cache的应答将直接从本地存储中获取。因为不需要网络连接,此类应答可以立即获取到。

 

2)有条件cache的应答必须在Web服务器验证一下cache的有效性。客户端发送一个请求,比如

“Give me /foo.png if it changed  since yesterday” , 服务端的应答要么是更新后的内容,要么是304 没有修改状态码。如果内容是没有改变的,就不需要下载了。

 

3)没有cache的应答将从服务器上获取。这些应答之后将存储在应答cache中。

 

可以使用”反射“(reflection)机制来使Https 应答cache功能在支持的设备上运行。以下示例代码将会在Ice Cream Sandwich上打开应答cache功能而不影响到早期的版本:

 

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) {
    }
}

 

当然,你也应该配置你的Web服务器,在其Http应答中设置cache头字段。

 

哪一个更好呢?

 

Apache HTTP client在Eclair(2.1)和Froyo(2.2)上bug更少,在这些系统版本上将是最佳选择。

 

在Gingerbread(2.3)系统及以后,HttpURLConnection将是最佳选择,其API简单,小巧,非常适合于Android。透明的压缩及应答cache机制减少了网络流量,改进了网络速度,节省了电力。 新的应用将应该使用HttpURLConnection, 它是Android团队以后花费精力不断推进的地方。

 

 

 

原文:

 

[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) < 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 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 HTTPS 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.

0
0
分享到:
评论
3 楼 songfantasy 2012-01-11  
ericbaner 写道

被墙了吗?
1 楼 songfantasy 2012-01-05  
原文在哪儿搞的?楼主?

相关推荐

Global site tag (gtag.js) - Google Analytics