http缓存Android
来源:互联网 发布:王家卫我爱你知乎 编辑:程序博客网 时间:2024/06/06 03:54
mvp + rxjava2 + retrofit demo
https://github.com/wanglianghai/CacheHttpDemo/tree/master/app/src/main
//重新读取网络数据拦截器 Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { //请求:无网络强制使用缓存 Request request = chain.request(); if (!NetConnect.isNetWorkConnect(MyApp.sContext)) { request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } //给自己应用看的头 Response response = chain.proceed(request); if (NetConnect.isNetWorkConnect(MyApp.sContext)) { return response.newBuilder() .removeHeader("Pragma") .header("Cache-Control", "public ,max-age=0") .build(); } else { int maxStale = 60 * 60 * 24 * 7; return response.newBuilder() .removeHeader("Pragma") .header("Cache-Control", "public, only-if-cache, max-style=" + maxStale) .build(); } } };
//设置缓存的大小和url File httpCacheFile = new File(MyApp.sContext.getCacheDir(), "responses"); // cache url int cacheSize = 50 * 1024 * 1024; // 50 MiB Cache cache = new Cache(httpCacheFile, cacheSize); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .cache(cache).build();
其他的看git的demo 讲一下主要的拦截器
看不的no内容:改造request请求(强制使用缓存)
阅读全文
1 0
- Android Http缓存数据处理
- Android Volley http缓存
- http缓存Android
- Android Http RequestCache缓存策略
- Android 4.0 Http缓存机制
- java/android http数据缓存
- Android 4.0 设置Http缓存的方式
- android--http请求及缓存框架GalHttprequest
- 当Android遇见HTTP缓存代理服务器
- Android HTTP请求下载图片并缓存
- Http缓存
- http缓存
- http缓存
- HTTP缓存
- Http缓存
- HTTP缓存
- http缓存
- HTTP-缓存
- DOM操作和BOM操作
- StringBuffer的用法
- Vue.js的安装与介绍
- 连接IBM MQ no mqjbnd in java.library.path错误解决方法
- Linux解决java.security.InvalidKeyException: Illegal key size or default parameters
- http缓存Android
- 数据结构上机测试4.1:二叉树的遍历与应用1
- UE4材质球
- nRF24L01介绍
- HDU 1013
- JavaScript(6)__Js基础<对象>
- Spring整合Quartz(Spring:4.2.6.RELEASE+Quartz 2.2.1)
- verilog中wire与reg类型的区别
- 代码code