博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)Android’s HTTP Clients
阅读量:6569 次
发布时间:2019-06-24

本文共 4020 字,大约阅读时间需要 13 分钟。

转载自: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

 and its sibling  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

 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. Work around this by disabling connection pooling:

privatevoid disableConnectionReuseIfNecessary(){
    // HTTP connection reuse which was buggy pre-froyo     if(Integer.parseInt(Build.VERSION.SDK)

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  shows how to disable it.

Since HTTP’s Content-Length header returns the compressed size, it is an error to use  to size buffers for the uncompressed data. Instead, read bytes from the response until  returns -1.

We also made several improvements to HTTPS in Gingerbread.  attempts to connect with  (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 a304 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:

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 ; it is where we will be spending our energy going forward.

转载于:https://www.cnblogs.com/greywolf/p/3228075.html

你可能感兴趣的文章
azure之MSSQL服务性能测试
查看>>
Android BitmapFactory.Options
查看>>
前端构建:Less入了个门
查看>>
phonegap(cordova) 自己定义插件代码篇(三)----支付宝支付工具整合
查看>>
linux 批量进行:解压缩某一类压缩文件类型的文件
查看>>
激活modelsim se 10.4 时运行patch_dll.bat不能生成TXT
查看>>
Node.js中针对中文的查找和替换无效的解决方法
查看>>
【Leetcode】Search in Rotated Sorted Array
查看>>
tomcat架构分析(valve源码导读)
查看>>
spring中InitializingBean接口使用理解(转)
查看>>
基于php5.5使用PHPMailer-5.2发送邮件
查看>>
InstallShield 2012 Spring新功能试用(16): Suite/Advanced UI 或 Advanced UI安装程序能在安装时进行输入合法性校验与反馈...
查看>>
C#面试宝典
查看>>
基金项目的英文
查看>>
《软件性能测试与LoadRunner实战教程》喜马拉雅有声图书上线
查看>>
ios 字典转模型
查看>>
Java类集
查看>>
类的生命周期
查看>>
php apache用户写文件夹权限设置
查看>>
浅析rune数据类型
查看>>