yale8848 / RetrofitCache

RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。
MIT License
676 stars 85 forks source link

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

Open swjtuhyq opened 4 years ago

swjtuhyq commented 4 years ago

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 commented 4 years ago

添加下面的代码试试

api.test().compose(CacheTransformer.emptyTransformer())...
swjtuhyq commented 4 years ago

这个放在什么地方呢?我把你的工程弄到本地,用来访问我的接口也不会缓存 访问地址: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 commented 4 years ago
  1. 你可以看看demo里的下面的函数封装 https://github.com/yale8848/RetrofitCache/blob/4d6a641d9d53a09e3d76ae7d588f6bf612a0d05c/app/src/main/java/ren/yale/android/retrofitcachetest/rx1/OKHttpUtilsRx1.java#L109
  2. MyInterceptor 里有没有对访问链接进行二次处理,比如说添加字段等,如果添加字段的话调用下面的代码:
 RetrofitCache.getInstance().addIgnoreParam("access_token");
swjtuhyq commented 4 years ago

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

yale8848 commented 4 years ago

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