Retrofit offline request and response

2020-04-17 06:41发布

I've already read many questions and answers about my issue, but I slill can't understand how to solve it.

I need to fetch response from server and store it in cache. After that when device is offline I want to use cached response. When device is online I want to fetch response exactly from server.

Looks not so complicated.

Here is the way (samples of code) I try to do this:

1)Method to create a Cache

Cache provideOkHttpCache() {
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(this.getCacheDir(), cacheSize);
        return cache;
    }

2)Create an Interceptor to modify cache-control headers

 Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {

            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365, TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();

            Request request = chain.request();

            if (isOnline()) {
                request = request.newBuilder()
                        .cacheControl(cacheControl)
                        .build();
            }
            okhttp3.Response originalResponse = chain.proceed(request);
            if (isOnline()) {
                int maxAge = 20; // read from cache
                okhttp3.Response response = originalResponse.newBuilder()
                        .header("cache-control", "public, max-age=" + maxAge)
                        .build();
                return response;
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                okhttp3.Response response = originalResponse.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
                return response;
            }
        }
    };

3) Create OkHttpClient and Retrofit

OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .cache(provideOkHttpCache())
            .build();

    retrofit = new Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl("https://randomuser.me/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

4) Method to make a network call

private void networkCall() {
    Log.v(TAG, "networkCall() is called");
    Call<RandomUsers> randomUsersCall = getRandomUserService().getRandomUsers(10);
    randomUsersCall.enqueue(new Callback<RandomUsers>() {
        @Override
        public void onResponse(Call<RandomUsers> call, @NonNull Response<RandomUsers> response) {
            if (response.isSuccessful()) {
                try {
                    Log.v(TAG, "headers = " + response.headers());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                mAdapter = new RandomUserAdapter();
                mAdapter.setItems(response.body().getResults());
                recyclerView.setAdapter(mAdapter);
            } else if (response.code() == 504) {
                Log.v(TAG, "response body = " + response.raw().cacheResponse());
            }
        }

        @Override
        public void onFailure(Call<RandomUsers> call, Throwable t) {
            Log.v("cache-control", "response failure");
        }
    });
}

So, here is the problem: The app never goes to the offline block of code in p.2. If max-age is still legal Retrofit use cached response. If response was cached some time ago and max-age is not legal there is two cases:

1) Device is online: Retrofit make new request to server (everything is OK)

2)Device is offline: Callback method onFailure inside networkCall() is called (p.4 of code samples)

What's wrong in my code? I really don't understand why the app cant use offline case of cache-control.

Sorry for too much text. Thanks!

1条回答
冷血范
2楼-- · 2020-04-17 07:25

Solved.

The trick is in combining Interceptor and NetworkInterceptor.

Steps:

1)Separate REWRITE_CACHE_CONTROL_INTERCEPTOR for two Interceptors, one for online work and other for offline work:

Interceptor OFFLINE_INTERCEPTOR = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (!isOnline()) {
                    int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                    request = request.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            .build();
                }

                return chain.proceed(request);
            }
        };


Interceptor ONLINE_INTERCEPTOR = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                okhttp3.Response response = chain.proceed(chain.request());
                 int maxAge = 60; // read from cache
                return response.newBuilder()
                         .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            }
        };

2)Add Interceptors to okHttpClient

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            //.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addInterceptor(OFFLINE_INTERCEPTOR)
            .addNetworkInterceptor(ONLINE_INTERCEPTOR)
            .cache(provideOkHttpCache())
            .build();

Link to original article

查看更多
登录 后发表回答