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



我正在使用Reformation2和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获取响应头Cachecontrol和Expires。

现在我想绕过Expires Header来获得服务器响应。请帮我解决这个问题。

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

Cache-Control: no-cache

您可以使用Reform的@Header或OkHttp拦截器来完成此操作。

最新更新