(转)Android’s HTTP Clients
转载自: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
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:
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, 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 Modifiedstatus. 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 use HttpURLConnection; it is where we will be spending our energy going forward.
(转)Android’s HTTP Clients的更多相关文章
- Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)
来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...
- [Android]Volley源码分析(四)
上篇中有提到NetworkDispatcher是通过mNetwork(Network类型)来进行网络访问的,现在来看一下关于Network是如何进行网络访问的. Network部分的类图:
- Android开源框架——Volley
Volley 是 Google 在 2013 I/O 大会上推出的 Android 异步网络请求框架和图片加载框架.特别适合数据量小,通信频繁的网络操作.Volley 主要是通过两种 Diapatch ...
- [Android Pro] 关于Android的HTTP客户端的小秘密
原文:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sinaa ...
- Android网络连接的两种方法:apache client和httpurlconnection的比较
另见http://blog.csdn.net/mingli198611/article/details/8766585 在官方blog中,android工程师谈到了如何去选择apache client ...
- android——网络操作(一)连接网络
连接网络 一,包含许可 <uses-permissionandroid:name="android.permission.INTERNET"/> <uses-pe ...
- 【Android Developers Training】 79. 连接到网络
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 聊聊HTTP gzip压缩与常见的Android网络框架
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/5835990.html 进入主题之前,我们先来看一下客户端与服 ...
- Android开发训练之第五章第七节——Transmitting Network Data Using Volley
Transmitting Network Data Using Volley GET STARTED DEPENDENCIES AND PREREQUISITES Android 1.6 (API L ...
随机推荐
- 基于mapreducer的图算法
作者现就职阿里巴巴集团1688技术部 引言 周末看到一篇不错的文章"Graph Twiddling in a MapReduce world" ,介绍MapReduce下一些图算法 ...
- Redis起步
Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介 Redis是一个key-value存储系统.和Memcached类似,但是解决 ...
- Struts的核心配置
一.配置struts.xml文件 1.struts.xml文件 2.常量配置 <constant> struts.properities web.xml中的<init-param&g ...
- css中的clear的正真意义
网上包括w3cschool 的说法都是 不允许浮动.消除浮动 这样的说法,却看了还是不知道什么意思,一些地方说不通. 所以找到w3c css 的英文文档,如下是节选: 'clear'Values ha ...
- Asp.Net WebAPI 通过HttpContextBase获取请求参数
WEBAPI中的Request是HttpRequestMessage类型,不能像Web传统那样有querystring和from 方法接收参数,而传统的HttpReqest的基类是HttpReqest ...
- JavaScript随机排序算法1
1.对数组循环,每一项与随机的某一项位置调换 <ul id="listOne"></ul> <div id="tempOne"&g ...
- easyui datagrid 行数
$('#gridList').datagrid('getData').rows.length
- android 开发工具(转)
一.Android SDK (Android SDK主安装包,包含SDK Manager.AVD Manager.工具包tools,释放后的根文件夹为android-sdk-windows): rev ...
- 旧版asp.net 发送邮件代码
说到发送邮件发送,先提一下SMTP(呵呵,高手就跳过这一段吧!). SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的地址传 ...
- OpenCV——识别手写体数字
这个是树莓派上运行的, opencv3 opencv提供了一张手写数字图片给我们,如下图所示,可以作为识别手写数字的样本库. 0到9共十个数字,每个数字有五行,一行100个数字.首先要把这5000个数 ...