转载自:http://blog.csdn.net/forever_crying/article/details/7021174
我是转载者.
一个就是大名鼎鼎的Apache HTTP Client, 而另外一个就是 HttpURLConnection.
因为已经有了一票APIs的存在, 所以Dalvik团队的同学们想要改进这个客户端并且不破坏其兼容性的情况下是非常非常困难滴! 并且Android团队的同学们也并没有负责Apache HTTP Client 的开发和维护!
在Gingerbread这个版本中, Dalvik团队的同学又添加了自动压缩数据的功能. 当你调用HttpURLConnection的时候,她回自动的偷偷的添加gzip属性到请求头中,并且会自己解压返回的数据, 开发者完全不用为了处理压缩数据而增加工作量, 只要服务器支持gzip就ok啦:
Accept-Encoding: gzip
而在 Ice Cream Sandwich版本中,Dalvik团队的同学又不安分了, 继续添加了一些新的特性: 响应缓存(response cache) . 如果使用了缓存,那么HTTP请求会有3种情况:
完全缓存的结果将直接从本地缓存中返回,省去了联网访问服务器的过程, 在中国的龟速移动网络环境中很有用哦
有条件(期限)的缓存将通过服务器来判断,客户端将发送这样一个请求”如果昨天得到的/foo.png这个图片已经更新了的话,就返回给我一个新的图片”,如果服务器更新了图片就返回新的数据 如果没有更新则返回”<code>304 Not Modified</code> ”.对于没有更新的内容就节约了流量.
对于没有缓存过的内容就直接请求服务器的数据,然后把这个结果再放到缓存中去.
如果您想在Ice Cream Sandwich版本中使用缓存这个有用的功能,而又不想在其他版本的系统中导致程序Crash 该怎么办!
这个时候Java强大的反射就再一次的挽救广大的开发者于水火之中! 代码如下:
如果有”android.net.http.HttpResponseCache“这个类 就使用缓存,没有就当没这回事, 该怎么滴就怎么滴了.
注意: 如果你想使用缓存, 还有配置你的服务器让她也支持缓存哦!
在Eclair 和 Froyo版本中, Apache HTTP Client具有更少的BUGs,所以应该在这个版本中用Apache.
而 对于Gingerbread 及其以后的版本中, HttpURLConnection 是最好的选择. 其简洁的API和轻量级的实现用于Android系统再适合不过了. 对开发者透明的压缩和缓存实现,可以减少网络数据传输量, 提高程序响应速度 同时也节约设备电源.
原文E文:
Posted by Tim Bray on 29 September 2011 at 9:17 AM

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.
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.
In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:
<code>Accept-Encoding: gzip</code>
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 <code>304 Not Modified</code> 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:
You should also configure your Web server to set cache headers on its HTTP responses.
Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/3738432.html如需转载请自行联系原作者
demoblog