转载自:http://android-developers.blogspot.com/2011/09/androids-http-clients.html

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

DefaultHttpClientand its siblingAndroidHttpClientare 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

HttpURLConnectionis 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, callingclose()on a readable InputStream couldpoison the connection pool. Work around this by disabling connection pooling:

privatevoid 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, theclass documentationshows how to disable it.

Since HTTP’sContent-Lengthheader returns the compressed size, it is an error to usegetContentLength()to size buffers for the uncompressed data. Instead, read bytes from the response untilInputStream.read()returns -1.

We also made several improvements to HTTPS in Gingerbread.HttpsURLConnectionattempts to connect withServer 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 a304NotModifiedstatus. 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:

privatevoid enableHttpResponseCache(){
try{
long httpCacheSize =10*1024*1024;// 10 MiB
File httpCacheDir =newFile(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 useHttpURLConnection; it is where we will be spending our energy going forward.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android引入外部自定义特殊字体的方法
  2. Androids中的System.loadLibrary对于依赖
  3. 大约Android远程监控APP源代码
  4. 开始我的Android开发之路
  5. 19条ANDROID平台设计规范平台设计规范
  6. Android实现ViewPager无限循环滚动回绕
  7. APM 原理与框架
  8. Android解决自定义View获取不到焦点的情
  9. android开发之Android(安卓)Interface De
  10. Android实现文章+评论(MVP,RxJava,Dagger2,But