【官方博客】Android’s HTTP Clients

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 could poison 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.

时间: 2024-08-28 08:36:19

【官方博客】Android’s HTTP Clients的相关文章

Android 官方博客 - Android应用程序的内存分析(翻译)(转)

转自:http://www.cnblogs.com/wisekingokok/archive/2011/11/30/2245790.html Dalvik虚拟机支持垃圾收集,但是这不意味着你可以不用关心内存管理.你应该格外注意移动设备的内存使用,在上面内存空间是受到限制的.在这篇 文章里面,我们来看看Android SDK里面的一些内存剖析工具(profiling tools)是如何帮助我们修整应用程序的内存使用. 一些内存使用问题是很明显的,例如,如果在每次用户触摸屏幕的时候应用程序有内存泄露

欢迎加入51CTO官方博客QQ群112163514

大家好,我是51CTO博客小管家--奋斗的小歌.来到51CTO这个大家庭已经两周时间了,认识了不少IT技术达人,你们在我心中真的是棒棒哒! 感谢小伙伴们对51CTO技术博客的支持,特此开放51CTO官方博客QQ群112163514,这里会定期发布51CTO博客最新公告.活动及动态. 如果你喜欢IT技术,喜欢探讨技术问题,喜欢交流学习经验,那加入我们一定没错.这里拥有大批IT技术人才和技术文章,精彩好玩的活动,这里,是你找到志同道合朋友的乐园! 还等什么,赶快加入我们的51CTO官方博客QQ群,踏

腾讯DBA官方博客开通了,欢迎交流

腾讯DBA官方博客开通了,欢迎交流哈..麻烦给放到首页一下 http://tencentdba.com 腾讯互娱游戏DBA团队一直致力于为游戏提供稳定.高效的DB运营服务,这是我们团队的使命. 过去DBA团队一直专注于业务本身,没有太多精力将我们的技术及解决方案对外输出,因此,对于开通团队Blog的事宜,之前的筹划被无限期搁置... 2015新年伊始,我们终于将这个事情提上日程!新的起点,我们愿意将过去积累的经验及教训,及未来的成长及进步点滴,都记录下来,与大家同分享.共成长. 小伙伴们,一起加

【官方博客】StrictMode API for Built-In Performance Monitoring

http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html StrictMode API for Built-In Performance Monitoring [This post is by Brad Fitzpatrick, an Android Software Engineer who worries unreasonably about responsiveness. —Tim

Dropbox 同步的工作方式,每个文件被分成4M的小块(官方博客)

http://www.zhihu.com/question/41544586 https://blogs.dropbox.com/tech/2014/07/streaming-file-synchronization/

div水平居中与垂直居中的方法【摘自美浩工作室官方博客 】

大家往往在写页面中会遇到不固定宽和高的div如果水平和垂直都居中呢?在写css的时候经常遇到的一个问题,当div没有固定的宽度或者高度的时候,如何才能让div水平或者垂直居中显示.如果div有固定宽度的话,用padding,margin都很容易实现.方法有很多种.不过经常遇到这种div没有固定的宽度高度的情况,我们就不能用margin,padding设置固定的距离了.这个问题让很多人头疼.而怎么样才能让这个div居中显示呢?其实这种情况解决的办法也是有很多种,js,css都可以实现. 1.先来看

程序猿的技术博客Android客户端--CSDN篇

2016年了,新的一年,新的开始 技术博客Android客户端也在慢慢的完善着 先上图 主要新增: 1.CSDN博客文章列表和详情 2.侧滑新增系统设置 3.系统设置新增离线缓存,默认缓存50页数据 4.TextView中代码样式调整显示 主要涉及知识点: 1.本地数据缓存sqlite 2.本地缓存读取和清除系统缓存[应用缓存和内存占用和文件缓存以及数据库缓存] 待完成: 1.图片加载和禁用 2.文章评论列表 3.文章分享 4.文章本地收藏 本段代码对TextView中代码段的处理,摘选自网上一

[Android编译(二)] 从谷歌官网下载android 6.0源码、编译并刷入nexus 6p手机

1 前言 经过一周的奋战,终于从谷歌官网上下载最新的android 6.0.1_r62源码,编译成功,并成功的刷入nexus6p,接着root完毕,现写下这篇博客记录一下实践过程. 2 简介 自己下载android系统源码,修改定制,然后编译刷入安卓手机,想想还有点小激动呢.简单点说一句话--定制我们自己的MIUI,这就是android的魅力,这篇博客博主就来教大家实现自己的定制系统. 首先,要明白下面的基础知识: (1) 什么是aosp? aosp就是android open source p

智能打印SDK---官方博客

(开源准备:也算是笔者从业以来第二个作品了,谢谢博客园编辑审核团队,欢迎置顶.欢迎转载.开源为百度云打包开源) 开源地址:http://pan.baidu.com/s/1miobLoO 电子商务越来越成熟,提供的服务越来越完善.在关键的发货打印快递单这个节点,总是出现各种瓶颈.主要问题是各种软件应用与打印机的交互,要么兼容差,要么效率低.针对这种情况,淘宝旗下的菜鸟物流,独自研发出了自己的打印组件--菜鸟打印组件. 但是这个组件只能在浏览器的极速模式下运行.兼容模式无法运行.导致现在国内主流的P