OkHttp 3 response cache (java.net.UnknownHostExcep

2019-03-04 05:54发布

问题:

I'm really stuck at this problem.

I want to cache server response for some time to use the data when device is offline. So I set a cache to my OkHttpClient. Also I set Cache-Contlol header.

Here is my code:

    public class MainActivity extends AppCompatActivity {

    Retrofit retrofit;
    RecyclerView recyclerView;
    RandomUserAdapter mAdapter;
    Cache cache;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();

        cache = provideOkHttpCache();

        Timber.plant(new Timber.DebugTree());

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                populateUsers();
            }
        });

        Interceptor cacheInterceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                okhttp3.Response originalResponse = chain.proceed(chain.request());
                okhttp3.Response modifyedResponse = originalResponse;

                if (true) {
                    modifyedResponse = originalResponse.newBuilder()
                            .header("Cache-Control", "max-age=86400")
                            .build();
                }

                return modifyedResponse;
            }
        };

        HttpLoggingInterceptor httpLoggingInterceptor = new
                HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(@NonNull String message) {
                Timber.i(message);
            }
        });

        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient()
                .newBuilder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(cacheInterceptor)
                .cache(cache)
                .build();

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

        populateUsers();

    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        button = findViewById(R.id.button);
    }

    private void populateUsers() {
        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()) {
                    Log.v("cache-control", "using cache = " + isFromCache(response));
                    mAdapter = new RandomUserAdapter();
                    mAdapter.setItems(response.body().getResults());
                    recyclerView.setAdapter(mAdapter);
                }
            }

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

    public RandomUsersApi getRandomUserService() {
        return retrofit.create(RandomUsersApi.class);
    }

    Cache provideOkHttpCache() {
        int cacheSize = 10 * 1024 * 1024; // 100 MiB
        File cacheDir = new File("cachedirectory");
        Cache cache = new Cache(cacheDir, cacheSize);
        return cache;
    }

    public boolean isFromCache(Response response) {
        return response.raw().cacheResponse() != null;
    }
}

Looks like everything is going to be ok. Then I do this:

  1. run app
  2. look at the OkHttp logs and on the screen to become sure that we have recieved data successfully
  3. close app
  4. turn on flight-mode on device (all networks off)
  5. run app

At this moment I expect that app is going to use cached data. But it doesn't. I have only blank screen and HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "randomuser.me": No address associated with hostname in my logcat.

回答1:

Well, I found the solution. I'm not sure why does it work, but it really does. First we need to modify creation of cacheInterceptor :

        Interceptor cacheInterceptor = 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 = 60; // read from cache
                    return originalResponse.newBuilder()
                            .header("Cache-Control", "public, max-age=" + maxAge)
                            .build();
                } else {
                    int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                    return originalResponse.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            .build();
                }
            }
        };

Then we need to use addNetworkInterceptor(Interceptor interceptor) instead addInterceptor(Interceptor interceptor):

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

The original answer:Caching with Retrofit 2.0 and okhttp3