HTTP Response Caching for Java and Android
来源:互联网 发布:java 调用实例的方法 编辑:程序博客网 时间:2024/05/22 07:00
If you find yourself using HttpClient, you can use HttpClient-Cache, which is an easy drop in for Java. See my previous post about HttpClient-Cache for Android. But if you’re using HttpUrlConnection (aka java.net.URL.openConnection()), there’s no good solution for regular Java or Android. Well, in Android 4.0 and later, you can use HttpResponseCache, but with only a small percentage of Android devices using 4.0 or later, that’s not a terribly good solution. If you use Android 4.0+’s HttpResponseCache as recommended by Google, then all previous Android versions end up with no HTTP response caching – this causes excess load on your servers, slower performance for the app, and unnecessary bandwidth use.
To fix this problem, I grabbed all the code from AOSP that implements Android 4.0′s HttpResponseCache and made it a separate library. This library is easy to use, works on Java 1.5+, all versions of Android, and is licensed under APLv2 (just like everything else in AOSP). Really, there’s no reason not to use it! You can even use in Java server applications, such as those that use Spring.
To use it, if you’re using Maven, simply add this block to your pom.xml (all artifacts are in Maven Central):
<dependency> <groupId>com.integralblue</groupId> <artifactId>httpresponsecache</artifactId> <version>1.2</version></dependency>If you’re not using Maven, you’ll need to add the httpresponsecache jar and its dependency, disklrucache.jar, to your project.
When your application starts, before it makes any HTTP requests, execute this method:
com.integralblue.httpresponsecache.HttpResponseCache.install(File directory, long maxSize);
If you’re using Android, and you want to use Android 4.0′s HttpResponseCache if it’s available, and fallback to this library if it’s not available:
final long httpCacheSize = 10 * 1024 * 1024; // 10 MiBfinal File httpCacheDir = new File(getCacheDir(), "http");try { Class.forName("android.net.http.HttpResponseCache") .getMethod("install", File.class, long.class) .invoke(null, httpCacheDir, httpCacheSize);} catch (Exception httpResponseCacheNotAvailable) { Ln.d(httpResponseCacheNotAvailable, "android.net.http.HttpResponseCache not available, probably because we're running on a pre-ICS version of Android. Using com.integralblue.httpresponsecache.HttpHttpResponseCache."); try{ com.integralblue.httpresponsecache.HttpResponseCache.install(httpCacheDir, httpCacheSize); }catch(Exception e){ Ln.e(e, "Failed to set up com.integralblue.httpresponsecache.HttpResponseCache"); }}The source code to the library is available on GitHub. I’m already using it in my CallerID Android app. If you end up using this library, please leave me a comment.
That’s it – enjoy easy to use HTTP caching!
相关链接:
HttpResponseCache
http-response-caching-for-java-and-android
http-response-caching-for-ios
- HTTP Response Caching for Java and Android
- Http Request and Http Response in Java
- A powerful image downloading and caching library for Android
- Picasso-A powerful image downloading and caching library for Android
- Browser Caching for GWT RPC Response
- XML HTTP Performance and Caching
- HTTP Request and Response
- Two Simple Rules for HTTP Caching
- Two Simple Rules for HTTP Caching
- Retrofit A type-safe HTTP client for Android and Java
- Retrofit A type-safe HTTP client for Android and Java
- Caching Tutorial for Web Authors and Webmasters
- http: get and response格式
- Thread-safe caching object with file and HTTP implementations
- Understanding the HTTP Vary Header and Caching Proxies (Squid, etc.)
- java.io.IOException: Server returned HTTP response code: 403 for URL: http://
- java.io.IOException: Server returned HTTP response code: 505 for URL: http://localhost:8080/fish/add
- java.io.IOException: Server returned HTTP response code: 403 for URL: http://
- c# 时间转yyyy/MM/dd格式
- 就这样 请安静的忘记我
- GDI+和GDI区别以及一些新特征
- TortoiseSVN在samba环境下提交代码出错的解决方法
- sharepoint2010---Turn on or turn off self-service site creation
- HTTP Response Caching for Java and Android
- UML图 符号的含义
- 多线程下非阻塞堆栈的实现
- 将字符赋给整形变量
- XSSer(超强XSS攻击利器)使用说明中文版
- Windows中使用DLL在进程间共享数据
- 模板是smarty,怎样用session语句,foreach语句来显示数组
- win7中如何干净的删除mysql 关键步骤
- c# xml与datatable操作