接口说明 管理HTTP缓存的功能主要由HttpResponseCache类提供。 // 初始化时设置缓存目录dir及最大缓存空间 HttpResponseCache.install(dir, 10 * 1024 * 1024); // 访问URL // 为确保缓存保存到文件系统可以执行 flush操作 HttpResponseCache.getInstalled().flush(); // 结束时关闭缓存 HttpResponseCache.getInstalled().close(
进行网络请求如果网络请求结果无效则使用缓存 //NETWORK:返回网络请求 initResponseSource(); //请求行为记录 if (responseCache instanceof HttpResponseCache ) { ((HttpResponseCache) responseCache).trackResponse(responseSource); } //请求资源需要访问网络 if (cachedResponseHeaders.validate(responseHeaders)) { if (responseCache instanceof HttpResponseCache ) { ((HttpResponseCache) responseCache).trackConditionalCacheHit(); }
当缓存被安装后(调用HttpResponseCache的install()方法),所有的HTTP请求都会满足以下三种情况: 所有的缓存响应都由本地存储来提供。 File httpCacheDir = new File(getCacheDir(), "http"); Class.forName("android.net.http.HttpResponseCache
File httpCacheDir = new File(getCacheDir(), "http"); Class.forName("android.net.http.HttpResponseCache
对网络的缓存可以参见HttpResponseCache。 备注:我们也可以自定义缓存的实现,一些网络库例如:Volley、Okhttp等都有好的实践供参考。
File baseDir = AppUtil.getAvailableCacheDir(sContext); final File cacheDir = new File(baseDir, "HttpResponseCache
对网络的缓存可以参见HttpResponseCache。 在某些情况,我们尽量少使用GPS定位,如果条件允许,尽可能使用网络定位。
当缓存被安装后(调用HttpResponseCache的install()方法),所有的HTTP请求都会满足以下三种情况: 所有的缓存响应都由本地存储来提供。