android 4.0加入了HttpResponseCache这个类来设置缓存.使用它的方式有2种:
1.
try {
// 单位KB
long httpCacheSize = 10 * 1024 * 1024;
// 缓存位置
File httpCacheDir = new File(getActivity().getCacheDir(), "http");
// 反射调用
Object result = Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
if (result instanceof HttpResponseCache) {
//设置为系统默认缓存
HttpResponseCache cacheObj = (HttpResponseCache) result;
Class.forName("java.net.ResponseCache")
.getMethod("setDefault", ResponseCache.class)
.invoke(null, cacheObj);
}
} catch (Exception httpResponseCacheNotAvailable) {
httpResponseCacheNotAvailable.printStackTrace();
}
2.
if (VERSION.SDK_INT >= 14) {
try {
// 单位KB
long httpCacheSize = 10 * 1024 * 1024;
// 缓存位置
File httpCacheDir = new File(getActivity().getCacheDir(),
"http");
ResponseCache.setDefault(HttpResponseCache.install(
httpCacheDir, httpCacheSize));
} catch (IOException e) {
e.printStackTrace();
}
}
怎么样 很简单吧):