Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为何我设置了也不走缓存呢,都是走的服务器? #40

Open
swjtuhyq opened this issue Oct 24, 2019 · 5 comments
Open

为何我设置了也不走缓存呢,都是走的服务器? #40

swjtuhyq opened this issue Oct 24, 2019 · 5 comments

Comments

@swjtuhyq
Copy link

swjtuhyq commented Oct 24, 2019

Mobile:

Android OS: 8.0

NetWork: WIFI

URL:

LIB Version:


OkHttpClient httpClient = new OkHttpClient.Builder()
.cache(cache)
// .cookieJar(new CookieManger(mContext.getApplicationContext()))
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
.addInterceptor(MyInterceptor)
.addInterceptor(new CacheForceInterceptorNoNet())
.addNetworkInterceptor(new CacheInterceptorOnNet())
.build();

mRetrofit = new Retrofit.Builder()
.baseUrl(mApiServer)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
RetrofitCache.getInstance().init(mContext.getApplicationContext()).setDefaultTimeUnit(TimeUnit.SECONDS).setDefaultTime(30);
RetrofitCache.getInstance().addRetrofit(mRetrofit);
mApiService = mRetrofit.create(IApiService.class);

@Cache(time = 10,timeUnit = TimeUnit.MINUTES)
@get("v1/robot")
Observable<ResultVO> getRobotInfo();

这是我的设置,不管我怎么设置时间,都不走缓存是什么原因,哪儿写错了吗?

@yale8848
Copy link
Owner

yale8848 commented Oct 24, 2019

添加下面的代码试试

api.test().compose(CacheTransformer.emptyTransformer())...

@swjtuhyq
Copy link
Author

swjtuhyq commented Oct 24, 2019

这个放在什么地方呢?我把你的工程弄到本地,用来访问我的接口也不会缓存
访问地址:https://dd.robelf.com/v1/states
日志:
2019-10-24 16:11:40.190 7831-7908/ren.yale.android.retrofitcachetest D/retrofitcache: ======== request: Request{method=GET, url=https://dd.robelf.com/v1/states, tags={class retrofit2.Invocation=ren.yale.android.retrofitcachetest.rx2.ApiRx2.getStates() []}}
======== request headers:
======= response header:server: nginx/1.13.2
content-type: application/json
x-powered-by: PHP/7.2.21
date: Thu, 24 Oct 2019 08:10:01 GMT
Cache-Control: public,max-age=0

这个跟服务器有关系吗?

@yale8848
Copy link
Owner

  1. 你可以看看demo里的下面的函数封装
    public static <T> Observable.Transformer<T, T> IoMain() {
  2. MyInterceptor 里有没有对访问链接进行二次处理,比如说添加字段等,如果添加字段的话调用下面的代码:
 RetrofitCache.getInstance().addIgnoreParam("access_token");

@swjtuhyq
Copy link
Author

swjtuhyq commented Oct 24, 2019

没有添加任何字段,我直接修改的demo里的Rx2 test
@Cache(time = 10,timeUnit = TimeUnit.MINUTES)
@get("https://dd.robelf.com/v1/states")
Observable<ResultVO> getStates();

@yale8848
Copy link
Owner

你看看LOGCAT TAG = retrofitcache 提示是否是从缓存加载的数据

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants