天天看點

[Android Pro] 關于Android的HTTP用戶端的小秘密

轉載自: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

[Android Pro] 關于Android的HTTP用戶端的小秘密

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

繼續閱讀