绕过okHttp Cache以获取网络响应,然后更新缓存

时间:2016-04-12 15:27:17

标签: android http retrofit okhttp http-caching

我使用Retrofit2和OKHttp缓存HTTP响应。 这是我的代码:

    int cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(application.getCacheDir(), cacheSize);

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().
cache(cache).addNetworkInterceptor(interceptor).build();

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();

我从后端REST API获取响应头Cache-control和Expires。

现在我希望通过绕过Expires Header来获取服务器响应。 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

将此标头添加到您的HTTP请求中:

Cache-Control: no-cache

您可以使用Retrofit的@Header或OkHttp interceptor执行此操作。

相关问题