Android’s HTTP Clients
来源:互联网 发布:天才小熊猫 知乎 编辑:程序博客网 时间:2024/06/06 05:44
转载自:http://android-developers.blogspot.com/2011/09/androids-http-clients.html
直接看不了,先Copy过来供有需要的先看,有时间在翻译。如果那位同人有兴趣,可以翻译分享给大家。
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.
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.
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.
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.
Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling close()
on a readable InputStream couldpoison the connection pool. Work around this by disabling connection pooling:
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:
Accept-Encoding: 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.
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.
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.
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
304 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.
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:
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.
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.
- Android's HTTP Clients
- Android’s HTTP Clients
- Android’s HTTP Clients
- Android’s HTTP Clients
- Android’s HTTP Clients && Android HttpURLConnection
- 【官方博客】Android’s HTTP Clients
- Android’s HTTP Clients(Android的HTTP客户端接口)
- Android's HTTP Clients -Android的http客户端连接方式
- Android’s HTTP Clients | Android Http通信中HTTPClient与HttpURLConnection的区别
- Android Async HTTP Clients: Volley vs Retrofit
- Android's Http Client
- C/S得回归,Smart Clients
- Android Camcorder--Camera clients conflict
- Android REST Clients 实践-官方实现
- Android REST Clients 实践-网络库
- Android REST Clients - WeakReference/AsyncTask Pattern
- ’HTTP(S)‘
- android系统内置HttpClient库(WebView+ Http(s)URLConnection(ok-http)+ HttpClient(apache-http))
- 又怎比得上日日朝夕相处的心情
- CSS浮动
- 小白自学Android路之 http协议
- Android 动态创建Android控件、绑定事件处理的新方法
- timetellyou 当我在荒废时间的时候有多少人在拼命
- Android’s HTTP Clients
- printf二进制数据
- 在使用SQL server High Availability 的SharePoint 2013 Farm环境中使用Import-SPMetadataWebServicePartitionData
- X264编码h264
- facebook的thriff 基于rpc的远程调用实现
- 南京林业大学“未来之星”第六届程序设计大赛(决赛)试题E 参考代码
- 怎么恢复手机错乱丢失的电话号码呢
- apache安全配置
- js刷新页面