Giter Site home page Giter Site logo

Comments (12)

yale8848 avatar yale8848 commented on August 25, 2024

本身就是这样的,你设置个缓存时间,在这段时间内只走缓存

from retrofitcache.

abcdisgreat avatar abcdisgreat commented on August 25, 2024

@yale8848
@get
@Cache(time = 7,timeUnit = TimeUnit.DAYS)
但是我这样设置的一个请求,连续访问都显示retrofitcache: get data from net,没有从缓存取啊,是我没设置对吗

from retrofitcache.

yale8848 avatar yale8848 commented on August 25, 2024

你详细看看README里的使用方法,每一步都要添加

from retrofitcache.

YuPf1989 avatar YuPf1989 commented on August 25, 2024

@yale8848 你解决了吗?我遇到了同样的问题,只有在网络断的时候log提示来自缓存,正常情况一直都是走网络

from retrofitcache.

yale8848 avatar yale8848 commented on August 25, 2024

把代码贴出来看看吧

from retrofitcache.

abcdisgreat avatar abcdisgreat commented on August 25, 2024

@YuPf1989 没有解决,作者说有这样的配置但我没找到,介绍里的方法在有网时都没能强制从缓存取

from retrofitcache.

YuPf1989 avatar YuPf1989 commented on August 25, 2024

创建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.

yale8848 avatar yale8848 commented on August 25, 2024

.compose(CacheTransformer.emptyTransformer()) 这个必须要加,你更新至1.0.9 试试

from retrofitcache.

YuPf1989 avatar YuPf1989 commented on August 25, 2024

更新过后试了试,在有网络的情况下,仍旧每次请求都提示from net,另外发现一个有意思的问题,在手机全部断开网络的情况下,并且有之前的缓存,如果超出了缓存时间,进行请求的时候,通过查看okhttp的log,会发现okhttp发起了网络请求,提示from net,之后再多次调用,retrofitcache log输出提示的是from cache,但是查看okhttp的log,仍然是进行了网络连接,状态码200(注意都是在没网的情况下),不知道是怎么一回事,为何没有网络http状态码会是200?是我哪里理解的不到位吗?

from retrofitcache.

yale8848 avatar yale8848 commented on August 25, 2024

调用:RetrofitCache.getInstance().init(Context) 了吗?

from retrofitcache.

YuPf1989 avatar YuPf1989 commented on August 25, 2024

调用了,现在在看okhttp缓存这一块,可能哪里理解出现了偏差

from retrofitcache.

yale8848 avatar yale8848 commented on August 25, 2024

okhttp 从缓存拿数据也是200

from retrofitcache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.