英文原文链接:http://android-developers.blogspot.sg/2011/09/androids-http-clients.html

今天面试被问到谷歌为何使用HttpURLConnection却弃用 Apache HttpClient,本人一脸茫然,瞬间变成小白,于是乎回家赶紧谷歌查看官方文档,一边翻译,一边学习一下,翻译不足之处,望大家指出。

原文标题:Android's HTTP Clients

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

大多数连接网络的 Android app 会使用 HTTP 来发送与接收数据。Android 提供了两种 HTTP clients:HttpURLConnection 与 Apache HttpClient。二者均支持 HTTPS、流媒体上传和下载、可配置的超时、IPv6 与连接池。

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.

DefaultHttpClient和它的同胞兄弟AndroidHttpClient应用于Web浏览器是高扩展的HTTP clients 。它们拥有强大而灵活的API。它们的实现是稳定的,与此同时他们也有一些bug。

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.

但正式由于这个API的庞大,使得我们很难在不破坏兼容性的基础上进行修改。 Android开发团队也不积极致力于的 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. HttpURLConnection是通用的,轻量级的HTTP client , 适合于大多数应用程序。 这个类出身卑微, 但其主要的API对于我们提高他的稳定性而言 变得很容易 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: 在Android Froyo这个版本以前, HttpURLConnection有很多令人崩溃的bug, 特别是 ,调用close()方法在一个 可读的InputStream中能够把连接池堵死。 解决这种情况得设置禁用连接池。
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: 在Gingerbread 这个版本中, 我们添加了易懂的响应压缩体, HttpURLConnection将会自动的添加这个表头在请求中,并处理相应的回答。 Accept-Encoding: gzip 接受编码: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. 配置您的Web服务器进行压缩响应, 带着 这个优势来 为客户服务保证客户端支持 服务端 如果响应压缩是有问题的,类文件会显示如何禁用它。
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. 自从 HTTP’s的报头返回压缩的值以来, 使用getContentLength()来衡量未压缩的数据的缓冲区大小 被证明是错误的用法 。替代的方法是,通过从返回的报文中  来读取字节 直到  InputStream.read() 返回-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. 在Gingerbread版本中我们也进行多次改进对于HTTPS, HttpsURLConnection试图通过允许多个HTTPS主机共享IP地址 连接SNI 它还支持压缩和会话的箭牌。一旦连接失败, 它会自动重试同时没有这些功能。 这使得HttpsURLConnection有效连接新的服务器时,不破坏旧兼容。

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 a304 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.

在Ice Cream Sandwich版本中, 我们添加一个响应缓存。拥有了缓存的HTTP请求将满足以下的一个到三个方面:
1.充满缓存的响应被直接服务从本地存储中, 因为没有网络连接需要做出这样的反应是立即可用。 2.条件缓存的响应必须是返回服务器验证过的最新的。客户端发送一个请求如 “Give me /foo.png if it changed since yesterday” ,此时服务器回复或者是已经更新的内容又或者是304未修改前状态。如果内容是不变的,它将不被下载! 3. 从web中返回了 未响应。这些回应会存储下来再在缓存后的响应。
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: 利用这些特征来启用HTTP响应缓存设备支持它。此示例代码运用响应缓存在 Ice Cream Sandwich版本中 ,而不影响之前的版本:
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. 你还应该配置您的Web服务器设置缓存HTTP响应头。

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.
Apache的HTTP client Eclair 和Froyo的错误比较少。是这几个版本的最佳选择,为了 Gingerbread及以后的版本 HttpURLConnection是最好的选择, 它的简单的 API 和小的尺寸使得它非常适合安卓。透明压缩和响应缓存减少网络使用,明显提高速度和节省电池。新的应用程序应使用HttpURLConnection;它是我们将要花费我们的精力去研究。
总结以上翻译: 谷歌支持HttpURLConnection原因有: 1.谷歌不愿意维护HTTPclient ,因为HTTPclient兼容性问题, 而支持HttpURLConnection 2.HttpURLConnection API简便而且包小,对安卓很合适 3.HttpURLConnection 对于提高速度和节省电池有帮助,同时谷歌也愿意在这方面花时间研究去更进一步的提高性能。

更多相关文章

  1. Android SDK目录及版本号区别
  2. Android API对应版本关系(最新更新2019年10月)
  3. Android仿人人客户端(v5.7.1)——对从服务器端(网络)获取的图片进行
  4. Android使用Bugly实现APP版本更新
  5. android:minSdkVersion 版本 标示
  6. Android性能:经典ListView适配器convertView缓存及复用机制

随机推荐

  1. 新建Android工程遇到几个问题
  2. Android(安卓)Intent的几种用法全面总结
  3. Android入门第十六篇之Style与Theme
  4. android每日一问【2011-09-27】
  5. Android(安卓)应用程序之间数据共享—Con
  6. Android中几种图像特效处理方法小结
  7. Android中Drawable Resource学习
  8. Android应用开发揭秘 第一章
  9. AIDL (Android(安卓)Interface Definitio
  10. Android灭亡论之Firefox OS操作系统出现