官方文档URL:http://developer.android.com/reference/android/net/http/HttpResponseCache.html
protectedvoid onCreate(Bundle savedInstanceState){
catch(IOException e){
...
try{
File httpCacheDir =newFile(context.getCacheDir(),"http");
long httpCacheSize =10*1024*1024;// 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
Log.i(TAG,"HTTP response cache installation failed:"+ e);
}
}
protectedvoid onStop(){
...
HttpResponseCache cache =HttpResponseCache.getInstalled();
if(cache !=null){
cache.flush();
}
}}
connection.addRequestProperty("Cache-Control","no-cache");
connection.addRequestProperty("Cache-Control","max-age=0");
only-if-cached
”指令try{
catch(FileNotFoundException e){
connection.addRequestProperty("Cache-Control","only-if-cached");
InputStream cached = connection.getInputStream();
// the resource was cached! show it
// the resource was not cached
}
}
int maxStale =60*60*24*28;// tolerate 4-weeks stale
connection.addRequestProperty("Cache-Control","max-stale="+ maxStale);
try{
catch(Exception httpResponseCacheNotAvailable){
File httpCacheDir =newFile(context.getCacheDir(),"http");
long httpCacheSize =10*1024*1024;// 10 MiB
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install",File.class,long.class)
.invoke(null, httpCacheDir, httpCacheSize);
}}
Android 缓存机制解决方案,布布扣,bubuko.com
原文:http://www.cnblogs.com/junj/p/3629724.html