- 浏览: 173356 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
hety163:
socket并不一定是长连接吧。。。
【转】关于socket长连接的心跳包 -
u013490012:
楼主,这个加阴影不是很清楚.我按照文章介绍的,程序出错啊
Android自定义Shape 加上阴影shadow之方法 -
cz2861856:
很好的文章!
Android自定义Shape 加上阴影shadow之方法 -
ezfantasy:
好文,转走了
Android 使用xliff 格式化字符串 -
songfantasy:
ericbaner 写道Android官方blog:http: ...
Android HTTP Clients使用选择介绍
翻译稿:
大多数需要网络连接的Android应用会使用HTTP协议发送和接收数据。Android包含两种Http客户端类, HttpURLConnection和Apache的HttpClient。这两者都支持HTTPS,streaming 上传与下载,配置超时时间,IPv6, 以及连接池。
Apache Http Client
DefaultHttpClient 和它的兄弟类AndroidHttpClient是可扩展的Http客户端,适用于Web浏览器。它们有大量的灵活的API,其实现是稳定的,极少有bug. 但是, 其巨大的API数量导致我们难以改进它们而不破坏兼容性。 Android团队现在已不活跃于在Apache HttpClient上的工作了。
HttpURLConnection
HttpURLConnection是一个适用于多数应用的通用的,轻量级的Http客户端。该类起初较粗糙,但其关注的API让我们容易稳固地改进它。 在Froyo(2.2)之前,HttpURLConnection有一些令人沮丧的的bug。特别是,在一个可读的InputStream上调用close()可阻碍连接池。绕过这个bug只能关掉连接池。
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");
}
}
在Gingerbread(2.3),我们增加了透明的应答压缩。HttpURLConnection将自动地将“Accept-Encoding: gzip”
头字段添加进上行的请求,并处理相应的应答。 通过配置你的Web服务器,对支持的客户端返回压缩后的应用数据。如果应用压缩是有问题的,可参见类文档如何关掉它。
因为HTTP的Content-Length头字段返回的是压缩后的大小, 使用getContentLength()来分配解压缩后数据的buffer 大小是错误的。 应该从应答中读字节直到InputStream.read()返回-1。
我们还在Gingerbread上对HTTPS作出了一些改进。HttpsURLConnection尝试以Server Name Indication(SNI)连接, SNI允许多个HTTPS host共享一个IP地址。HttpsURLConnection还具备压缩和会话ticket功能。没有这些功能,一旦连接失败,它会自动重试。这使得HttpsURLConnection有效地连接最新的服务器,同时不破坏对老旧服务器的兼容性。
在Ice Cream Sandwich(2.4 or 4.0),我们增加了应答cache。配备了cache后,HTTP 请求以以下三种方式之一处理:
1)全cache的应答将直接从本地存储中获取。因为不需要网络连接,此类应答可以立即获取到。
2)有条件cache的应答必须在Web服务器验证一下cache的有效性。客户端发送一个请求,比如
“Give me /foo.png if it changed since yesterday” , 服务端的应答要么是更新后的内容,要么是304 没有修改状态码。如果内容是没有改变的,就不需要下载了。
3)没有cache的应答将从服务器上获取。这些应答之后将存储在应答cache中。
可以使用”反射“(reflection)机制来使Https 应答cache功能在支持的设备上运行。以下示例代码将会在Ice Cream Sandwich上打开应答cache功能而不影响到早期的版本:
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) {
}
}
当然,你也应该配置你的Web服务器,在其Http应答中设置cache头字段。
哪一个更好呢?
Apache HTTP client在Eclair(2.1)和Froyo(2.2)上bug更少,在这些系统版本上将是最佳选择。
在Gingerbread(2.3)系统及以后,HttpURLConnection将是最佳选择,其API简单,小巧,非常适合于Android。透明的压缩及应答cache机制减少了网络流量,改进了网络速度,节省了电力。 新的应用将应该使用HttpURLConnection, 它是Android团队以后花费精力不断推进的地方。
原文:
[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 HTTPS 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.
评论
被墙了吗?
发表评论
-
【转】eclipse 找不到Courier New 样式解决方法
2013-07-13 17:24 1000之前有朋友问到说eclipse3.7,以及以后版本(貌似直 ... -
【转】Android使用ant对项目进行编译签名优化打包
2013-05-14 10:42 1032Android本身是支持ant打包项目的,并且SDK中自带一个 ... -
Android一些常用的颜色值
2013-05-09 17:30 1072一些常用的颜色值: <?xml version= ... -
【转】Android jar resource 资源文件的获取
2013-04-17 23:56 4427jar引用的资源文件必须放在assets文件夹中,并且布局文 ... -
【转】Android导出jar library时资源resource id获取的问题
2013-04-17 23:50 4199当需要把一个Android导出为jar包时,如果里面有用 ... -
【转】Android类库打包方法探究
2013-04-17 10:51 829开发Android应用的时候 ... -
【转】Android编程之解决android-support-v4打包问题
2013-03-11 19:21 1065如果工程引入了android-s ... -
【转】Android NIO Socket编程
2013-03-05 20:45 1623根据前两周写的关于Socket编程的网络通信的代码,现在对有 ... -
【转】关于socket长连接的心跳包
2013-03-05 17:39 2257出于最近对im研究的兴趣,看到smack里有个30s发送一个 ... -
【转】java nio对OP_WRITE的处理解决网速慢的连接
2013-03-05 15:20 200417.3.1 如何处理慢速的 ... -
【转】Java 加密解密基础
2013-01-24 10:54 995Java 加密解密基础 密码学是研究编制密码和破译密码的 ... -
【转】RSA选用小公钥指数(e=3)真的不安全吗?
2013-01-24 10:04 2880引言现有的大部分RSA算法实现都遵循PKCS#1 v2.1/ ... -
【转】RSA加密的填充模式
2013-01-24 10:02 4924跟DES,AES一样, RSA也是一个块加密算法( bloc ... -
【转载】RSA数字签名与加密、解密间的关系
2013-01-22 22:02 1178转载提及RSA,大家 ... -
ubuntu 10.04LTS安装sun java6 jdk问题解决
2013-01-07 18:01 1209unbuntu10.04安装java6问题 Packag ... -
异步线程干活和回调 的设计模式
2012-12-04 12:24 0老板要干事情通过开启代理线程进行,老板有一个回调函数代理(苦力 ... -
如何成为JAVA大牛?
2012-12-04 11:19 0如何成为JAVA大牛? 首先要有目标的去学习,我给 ... -
经典JAVA面试题
2012-11-29 10:26 0基础: 1. JAVA的数据类型可以分成哪两类? 原始 ... -
接口与抽象类
2012-11-27 23:22 1028以下为选摘自网络,罗 ... -
android 使用Application保存全局变量
2012-10-29 17:37 1704三个步骤,将全局对象保存在Application进程。 ...
相关推荐
[x] 支持H264硬解码显示(Android 4.1以上版本支持) [ ] 支持H264软解码显示 > - [ ] 支持ACC音频 使用方法 使用Jcenter仓库 > compile "com.aaronhan:rtspclient:0.7" 调用RtspClient方法 private ...
http_client for android 工具包
// 1.创建异步请求的客户端对象 ...其他有什么问题或者想具体了解详细说明,可以参考官网 http://loopj.com/android-async-http/ 其他参考链接 http://blog.csdn.net/redarmy_chen/article/details/26980613
androdns Android DNS Client Google Play: https://play.google.com/store/apps/details?id=androdns.android.leetdreams.ch.androdns
mqtt android client require jar
Android TCP Client demo send receive,Android 4.4.4测试通过
android tcp协议 手机与手机之间通信 在路由器下测试没问题(非对称的网络) 其他的移动网络没试 就是一个思路
Android HTTP客户端 Android Http Client是一个小型库,用于以简单实用的方式向任何Internet服务发出请求。 您可以实现多个接口来管理响应。 它还包括用于管理文件上载和... JSON :Android Http Client使用获取兼容性
Android TCP客户端源码 博文地址 https://blog.csdn.net/lyndon_li/article/details/82263172
socket client 客户端线程接收server数据,看了网上的一个socket通信都是客户端发给服务器的,很少有服务器发给客户端的,所以上传了这个服务器发给客户端的例子。
出校控制器IPClient的Android版,2014年8月,支持桂林电子科技大学及广西师范大学
LogDNA-Android-Client Android client for LogDNA Add the JitPack repository to your build file allprojects { repositories { ... maven { url 'https://jitpack.io' } } } Add dependency dependencies ...
华为 secoclient 客户端 7.0.5.1 for android
Android手机游戏客户端Client源代码 毕业设计;Android手机游戏客户端Client源代码
Android socket 学习记录 client端源码,Android socket 学习记录 之 执行new socket(ip, port)程序崩溃这篇文章的APP源码,免积分下载。
SecoClient 7.0.2.26 移动客户端 包括 Android IOS 安装包 secoclient-android-ios-7.0.2.26
Funambol Sync Client for Android is a client to synchronize PIM Data of Android devices with any SyncML aware server. For the moment, it is an experimental project to explore the possibility of the...
jpush android client
Android开发,Socket Client端和Socket Server端数据发送和接收
本《适用于 Android 的 VMware Horizon Client 用户指南》文档介绍如何通过适用于 Android 的 VMware Horizon® Client™ 连接并使用远程桌面和已发布应用程序。 Horizon Client 可充当客户端设备与远程桌面和已发布...