热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Google为何弃用HttpClient的而推荐使用HttpURLConnection

先说原因:因为兼容性问题,谷歌不愿意维护HttpClient,而使用HttpURLConnectionHttpURLConnection的API包小而简便,更适合安卓Http

先说原因:

  1. 因为兼容性问题,谷歌不愿意维护HttpClient,而使用HttpURLConnection
  2. HttpURLConnection的API包小而简便,更适合安卓
  3. HttpURLConnection能够提高速度和提升电池性能

这是原文地址:

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

以下是原文和翻译:

Android’s HTTP Clients

Jesse Wilson
[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应用会使用HTTP来发送和接收数据。Android提供了两种HTTP客户端:HttpURLConnection和Apache HTTP Client。二者均支持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客户端,它们拥有庞大而灵活的APIs,它们的实现很稳定并且很少有错误。

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客户端。这个类出身卑微,但是它主要的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:

在Froyo之前的版本中,HttpURLConnection有许多令人崩溃的错误。尤其是在一个可读的InputStream中调用close()会堵死连接池。通过禁用连接池解决此问题:

private void disableConnectionReuseIfNecessary() {
// HTTP connection reuse which was buggy pre-froyo
if (Integer.parseInt(Build.VERSION.SDK) 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的Content-Length的头返回压缩后的大小,通过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试图连接服务器名称指示(SNI),它允许多个HTTPS站点在相同的IP地址上。它还支持压缩和会话票证。如果连接失败,则会在没有这些功能的情况下自动重试。这使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:

在Ice Cream Sandwich中,我们增加了响应缓存。 安装缓存后,HTTP请求将通过以下三种方式之一得到满足:

  • 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!

  • 有条件高速缓存的响应必须由Web服务器验证其新鲜度。 客户端发送类似“给我/foo.png从昨天开始,如果它改变了”,而服务器或者更新的内容或答复的请求304 Not Modified的状态。 如果内容不变,则不会下载!

  • 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:

使用反射在支持它的设备上启用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.

Apache HTTP客户端在Eclair和Froyo上有更少的错误。 它是这些版本的最佳选择。

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.

对于Gingerbread和之后的版本,HttpURLConnection是最好的选择。 它的简单的API和小尺寸使它非常适合Android。 透明压缩和响应缓存减少网络使用,提高速度和节省电池。 新的应用程序应该使用HttpURLConnection; 这是我们将在未来花费我们的能量的地方。

水平有限,翻译不足之处,望大家指出,多谢!


推荐阅读
author-avatar
南非酋长
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有