【问题标题】:Retrofit Cache Data for Offline Usage改造缓存数据以供离线使用
【发布时间】:2017-08-25 06:44:18
【问题描述】:

我正在使用 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);
        }
    }

应用级别: 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'
}

【问题讨论】:

  • 使用 sqlite 数据库
  • 你可以使用 sql lite 或 relm 数据库
  • 我相信可以在 Retrofit 本身中缓存数据以供离线使用...
  • A video recap 你想得到什么。

标签: android android-recyclerview retrofit


【解决方案1】:

让我们先构建一个 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", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build();
      }
      return chain.proceed(request);
    }
  })
  .build();

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

然后拦截器使用我的应用程序类中的实用方法来检查连接。如果有连接,我们告诉请求它可以重用数据 60 秒。

如果没有连接,我们要求仅提供(仅在缓存时)最多 7 天前的“陈旧”数据。

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

【讨论】:

  • 很好......解释得很好,没有混淆,没有怀疑,除了我需要在我的应用程序中使用这个代码......我需要更新到 Retrofit 2.0 而不是 1.9在 build.gradle... 请检查上面发布的代码和我的 gradle 表,让我知道您的建议。
  • 如果我们保存数据 60 秒,那么如果离线,60 秒后数据如何可用?它不会在 60 秒后从缓存中自动删除吗?
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 2015-09-28
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
  • 2019-03-25
相关资源
最近更新 更多