Comments (12)
本身就是这样的,你设置个缓存时间,在这段时间内只走缓存
from retrofitcache.
@yale8848
@get
@Cache(time = 7,timeUnit = TimeUnit.DAYS)
但是我这样设置的一个请求,连续访问都显示retrofitcache: get data from net,没有从缓存取啊,是我没设置对吗
from retrofitcache.
你详细看看README里的使用方法,每一步都要添加
from retrofitcache.
@yale8848 你解决了吗?我遇到了同样的问题,只有在网络断的时候log提示来自缓存,正常情况一直都是走网络
from retrofitcache.
把代码贴出来看看吧
from retrofitcache.
@YuPf1989 没有解决,作者说有这样的配置但我没找到,介绍里的方法在有网时都没能强制从缓存取
from retrofitcache.
创建okhttp部分
`
private RetrofitHelper() {
retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MyGsonConverterFactory.create())
.client(getOkHttpClient())
.build();
RetrofitCache.getInstance().addRetrofit(retrofit);
}
private OkHttpClient getOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 200M
int cacheSize = 200 * 1024 * 1024;
File httpcacheFile = new File(mContext.getCacheDir(), "httpcache");
Cache cache = new Cache(httpcacheFile, cacheSize);
builder
.cache(cache)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new CacheForceInterceptorNoNet())
.addNetworkInterceptor(new CacheInterceptorOnNet())
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
return builder.build();
}`
接口部分
@Cache(time = 2,timeUnit = TimeUnit.MINUTES,forceCacheNoNet = true) @GET("http://47.93.136.56:7011/appInterface/patient/getWikiList.html") Observable<NewsListBean> getWikiList2();
调用部分
` RetrofitHelper.getInstance().retrofit.create(RetrofitService.class).getWikiList2()
.compose(CacheTransformer.emptyTransformer())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new MyObserver() {
@Override
public void onNext(NewsListBean data) {
String content = data.toString();
tvContent.append("content:" + content + "\n");
}
@Override
public void onError(Throwable e) {
super.onError(e);
}
});`
log输出
09-26 17:23:36.551 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:38.436 319-637/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=27.33 dur=2048.96 max=1156.41 min=8.22 09-26 17:23:38.500 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:38.555 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:39.495 319-1177/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=51.91 dur=1059.44 max=163.82 min=8.15 09-26 17:23:39.572 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:39.629 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200
不知道哪个环节出了问题?
还有必须添加 .compose(CacheTransformer.emptyTransformer())吗?
from retrofitcache.
.compose(CacheTransformer.emptyTransformer()) 这个必须要加,你更新至1.0.9 试试
from retrofitcache.
更新过后试了试,在有网络的情况下,仍旧每次请求都提示from net,另外发现一个有意思的问题,在手机全部断开网络的情况下,并且有之前的缓存,如果超出了缓存时间,进行请求的时候,通过查看okhttp的log,会发现okhttp发起了网络请求,提示from net,之后再多次调用,retrofitcache log输出提示的是from cache,但是查看okhttp的log,仍然是进行了网络连接,状态码200(注意都是在没网的情况下),不知道是怎么一回事,为何没有网络http状态码会是200?是我哪里理解的不到位吗?
from retrofitcache.
调用:RetrofitCache.getInstance().init(Context) 了吗?
from retrofitcache.
调用了,现在在看okhttp缓存这一块,可能哪里理解出现了偏差
from retrofitcache.
okhttp 从缓存拿数据也是200
from retrofitcache.
Related Issues (20)
- 有网络先加载缓存,再加载网络 HOT 15
- 报错NoSuchMethodException HOT 4
- 后台规定全用post HOT 3
- release版本不能获取mock数据么 HOT 7
- 如何在下拉刷新的时候不走缓存. HOT 6
- 设置缓存时间在使用CacheForceInterceptorNoNet的时候无效?
- 请求添加对协程的支持 HOT 1
- rxjava2的库mock不了 HOT 5
- 大佬什么时候更新支持最新retrofit2啊? HOT 4
- Mock 与CacheForceInterceptorNoNet HOT 7
- 当使用【zip】操作符合并网络请求时,MOCK注解失效
- 为何我设置了也不走缓存呢,都是走的服务器? HOT 5
- 缓存只支持get请求吗,post不支持吗 HOT 2
- 设置忽略某个参数,可以做成针对不同接口来设置吗
- ren.yale.android.retrofitcachelibrx2.intercept.CacheForceInterceptorNoNet.intercept
- @Cache(time = 10) //缓存设置为10秒 设置10秒无效,缓存一直在
- 能通过修改哪块代码来支持post请求吗
- 不支持https?? HOT 2
- retrofit 2.6版本此库失效 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from retrofitcache.