热门标签 | 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; 这是我们将在未来花费我们的能量的地方。

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


推荐阅读
  • 参考资料:http:www.systinet.comdocwasp_uddiuddiigpreliminary.html教程中的一个例程,可以下载。来源:竹笋炒肉虽然用telnet这样的程 ... [详细]
  • packagecom.example.uptoserverdemo;importjava.io.File;importjava.io.IOException;importorg.ap ... [详细]
  • 本篇文章给大家分享的是有关如何正确的使用HttpClient方法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不 ... [详细]
  • Android本地化存储Cookie(针对HttpClient)
    因为最近有人问我怎么保存HttpClient的Cookie,所以这里写下,顺便记录总结吧.当然,有Android网络编程经历的童鞋一看就懂喇~就不多说了,直接上代码: ... [详细]
  • AComparisonofjava.net.URLConnectionandHTTPClientSincejava.net.URLConnectionandHTTPClienthav ... [详细]
  • HttpClient请求https的实例:packagetrain;importjava.io.IOException;importjava.security.NoSuchAlg ... [详细]
  • Android程序在长时间休眠后HttpClient不工作用bugreport看线程信息都是在awaitConnectTask#3prio5tid16WAIT ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文总结了初学者在使用dubbo设计架构过程中遇到的问题,并提供了相应的解决方法。问题包括传输字节流限制、分布式事务、序列化、多点部署、zk端口冲突、服务失败请求3次机制以及启动时检查。通过解决这些问题,初学者能够更好地理解和应用dubbo设计架构。 ... [详细]
  • 最近手上在进行一个性能测试项目,脚本是java语言使用httpClient实现http请求。并发用户数线程只有40个,但是服务器端启动的线程出现了400多个,是哪里平白无故出现这么多线程呢?肯定是有问 ... [详细]
  • 如何使用.NET CORE HttpClient
    小编这次要给大家分享的是如何使用.NETCOREHttpClient,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。前 ... [详细]
  • 在Java领域,谈到网络编程,可能大家脑海里第一反应就是MINA,NETTY,GRIZZLY等优秀的开源框架。没错,不过在深入探究这些框架之前,我们需要先从最original的技 ... [详细]
  • 今天就跟大家聊聊有关HttpComponents中如何使用HttpClient连接池,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大 ... [详细]
  • Eclipse利用HttpClient 写post和get连接到后台
    文件目录如下:第一个包代码如下:packagecn.itcast.login;importcn.itcast.login.service.DataService;importandroid.ap ... [详细]
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社区 版权所有