android – 用于离线使用的改进缓存数据

android – 用于离线使用的改进缓存数据,第1张

概述我正在使用Retrofit库来解析并将 JSON数据填充到RecyclerView中. 但是,现在我想知道,我如何为离线使用存储相同的数据(甚至互联网不可用). @Override public void onResume() { super.onResume(); subscription = sampleAPI.getSamples() 我正在使用Retrofit库来解析并将 JSON数据填充到RecyclerVIEw中.

但是,现在我想知道,我如何为离线使用存储相同的数据(甚至互联网不可用).

@OverrIDe    public voID onResume() {        super.onResume();        subscription = sampleAPI.getSamples()                .cache()                .timeout(5000,TimeUnit.MILliSECONDS)                .retry(1)                .doOnUnsubscribe(new Action0() {                    @OverrIDe                    public voID call() {                        Log.d(getClass().getname(),"Called unsubscribe OnPause()");                    }                })                .observeOn(AndroIDSchedulers.mainThread())                .subscribe(new Action1<SampleTypePojo>() {                               @OverrIDe                               public voID call(SampleTypePojo jokesModel) {                                   sampleList = jokesModel.getValue();                                   displaySampleList(sampleList);                               }                           },new Action1<Throwable>() {                               @OverrIDe                               public voID call(Throwable throwable) {                                   Log.e(getClass().getname(),"ERROR: " + throwable.getMessage());                                   throwable.printstacktrace();                               }                           }                );    }    private voID getSampleData() {        if (sampleAPI == null) {            sampleAPI = new RestAdapter.Builder()                    .setEndpoint(Constants.BASE_URL)                    .setLogLevel(RestAdapter.LogLevel.FulL)                    .build()                    .create(SampleAPI.class);        }    }

app level:build.gradle

dependencIEs {    compile 'com.squareup.retrofit:retrofit:1.9.0'    compile 'io.reactivex:rxjava:1.0.4'    compile 'io.reactivex:rxandroID:0.24.0'}
解决方法 让我们首先构建一个OKhttp客户端

缓存
检查连接的拦截器,如果没有请求缓存数据:
这是客户.

OkhttpClIEnt clIEnt = new OkhttpClIEnt  .Builder()  .cache(new Cache(App.sApp.getCacheDir(),10 * 1024 * 1024)) // 10 MB  .addInterceptor(new Interceptor() {    @OverrIDe public Response intercept(Chain chain) throws IOException {      Request request = chain.request();      if (App.isNetworkAvailable()) {        request = request.newBuilder().header("Cache-Control","public,max-age=" + 60).build();      } else {        request = request.newBuilder().header("Cache-Control",only-if-cached,max-stale=" + 60 * 60 * 24 * 7).build();      }      return chain.proceed(request);    }  })  .build();

我们首先创建10 MB的缓存对象,从静态应用程序上下文中获取缓存目录.

然后Interceptor在我的Application类中使用实用程序方法来检查连接.如果有连接,我们告诉请求它可以重复使用数据60秒.

如果没有连接,我们要求在7天前仅给出(仅限缓存)“陈旧”数据.

现在让这个Okhttp客户端成为Retrofit2的客户端,当应用程序离线时,您将能够使用旧的缓存数据

总结

以上是内存溢出为你收集整理的android – 用于离线使用的改进缓存数据全部内容,希望文章能够帮你解决android – 用于离线使用的改进缓存数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存