脱机时可以使用OKHttp进行改造以使用缓存数据


148

我正在尝试使用Retrofit和OKHttp来缓存HTTP响应。我遵循了要点,最后得到了以下代码:

File httpCacheDirectory = new File(context.getCacheDir(), "responses");

HttpResponseCache httpResponseCache = null;
try {
     httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024 * 1024);
} catch (IOException e) {
     Log.e("Retrofit", "Could not create http cache", e);
}

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(httpResponseCache);

api = new RestAdapter.Builder()
          .setEndpoint(API_URL)
          .setLogLevel(RestAdapter.LogLevel.FULL)
          .setClient(new OkClient(okHttpClient))
          .build()
          .create(MyApi.class);

这是MyApi,带有Cache-Control标头

public interface MyApi {
   @Headers("Cache-Control: public, max-age=640000, s-maxage=640000 , max-stale=2419200")
   @GET("/api/v1/person/1/")
   void requestPerson(
           Callback<Person> callback
   );

首先,我在线请求并检查缓存文件。正确的JSON响应和标头在那里。但是,当我尝试离线请求时,我总是得到RetrofitError UnknownHostException。我还需要做些其他事情来使Retrofit从缓存中读取响应吗?

编辑: 由于OKHttp 2.0.x HttpResponseCacheCachesetResponseCachesetCache


1
您调用的服务器是否以适当的Cache-Control标头响应?
哈桑·易卜拉欣2014年

它返回这个,Cache-Control: s-maxage=1209600, max-age=1209600我不知道是否足够。
osrl 2014年

似乎该public关键字需要在响应标头中才能使其脱机工作。但是,这些标头不允许OkClient在可用时使用网络。无论如何,是否可以将缓存策略/策略设置为在可用时使用网络?
osrl 2014年

我不确定您是否可以在同一请求中执行此操作。您可以检查相关的CacheControl类和Cache-Control标头。如果没有这种行为,我可能会选择发出两个请求,一个仅缓存的请求(仅if-if-cached),然后是一个网络请求(max-age = 0)。
哈桑·易卜拉欣2014年

那是我想到的第一件事。我花了几天的时间在CacheControl和CacheStrategy类中。但是两个请求的想法没有多大意义。如果max-stale + max-age通过,它确实会从网络发出请求。但我想设置一个最大过时的一周。即使有可用的网络,它也会从缓存中读取响应。
osrl 2014年

Answers:


189

编辑改造2.x:

OkHttp Interceptor是脱机时访问缓存的正确方法:

1)创建拦截器:

private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
    @Override public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        if (Utils.isNetworkAvailable(context)) {
            int maxAge = 60; // read from cache for 1 minute
            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();
        }
    }

2)安装客户端:

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);

//setup cache
File httpCacheDirectory = new File(context.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);

//add cache to the client
client.setCache(cache);

3)将客户添加到改造中

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

另请检查@kosiara-Bartosz Kosarzycki答案。您可能需要从响应中删除一些标头。


OKHttp 2.0.x(检查原始答案):

由于OKHttp 2.0.x HttpResponseCacheCache,所以setResponseCachesetCache。因此,您应该setCache这样:

        File httpCacheDirectory = new File(context.getCacheDir(), "responses");

        Cache cache = null;
        try {
            cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
        } catch (IOException e) {
            Log.e("OKHttp", "Could not create http cache", e);
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        if (cache != null) {
            okHttpClient.setCache(cache);
        }
        String hostURL = context.getString(R.string.host_url);

        api = new RestAdapter.Builder()
                .setEndpoint(hostURL)
                .setClient(new OkClient(okHttpClient))
                .setRequestInterceptor(/*rest of the answer here */)
                .build()
                .create(MyApi.class);

原始答案:

事实证明,服务器响应必须Cache-Control: public作出OkClient从缓存中读取。

另外,如果您想在可用时向网络请求,则应添加Cache-Control: max-age=0请求标头。此答案显示了如何对其进行参数化。这是我的用法:

RestAdapter.Builder builder= new RestAdapter.Builder()
   .setRequestInterceptor(new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json;versions=1");
            if (MyApplicationUtils.isNetworkAvailable(context)) {
                int maxAge = 60; // read from cache for 1 minute
                request.addHeader("Cache-Control", "public, max-age=" + maxAge);
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                request.addHeader("Cache-Control", 
                    "public, only-if-cached, max-stale=" + maxStale);
            }
        }
});

(我想知道为什么这行不通;结果我忘了设置OkHttpClient使用的实际缓存。请参阅问题或此答案中的代码。)
Jonik 2015年

2
建议只是一个字: HttpResponseCache has been renamed to Cache.** Install it with OkHttpClient.setCache(...) instead of OkHttpClient.setResponseCache(...)
Henrique de Sousa

2
网络不可用时,我不会调用拦截器。我不确定网络不可用时的情况将如何发生。我在这里想念什么吗?
Androidme 2016年

2
if (Utils.isNetworkAvailable(context))正确的还是应该逆转,即if (!Utils.isNetworkAvailable(context))
ericn '16

2
我正在使用Retrofit 2.1.0,当电话离线时,public okhttp3.Response intercept(Chain chain) throws IOException从不打电话,只有在我在线时才打电话
ericn '16

28

上面所有的答案都不适合我。我尝试在改造2.0.0-beta2中实现离线缓存。我添加了一个使用okHttpClient.networkInterceptors()方法的拦截器,但是java.net.UnknownHostException当我尝试离线使用缓存时收到了。原来,我也必须添加okHttpClient.interceptors()

问题在于缓存没有写到闪存中,因为服务器返回了Pragma:no-cache,这阻止了OkHttp存储响应。即使修改了请求标头值,脱机缓存也无法正常工作。经过一番尝试后,我通过不从请求而不是从请求中除去编译指示来使高速缓存可以正常工作,而无需修改后端-response.newBuilder().removeHeader("Pragma");

改装:2.0.0-beta2;OkHttp:2.5.0

OkHttpClient okHttpClient = createCachedClient(context);
Retrofit retrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl(API_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
service = retrofit.create(RestDataResource.class);

...

private OkHttpClient createCachedClient(final Context context) {
    File httpCacheDirectory = new File(context.getCacheDir(), "cache_file");

    Cache cache = new Cache(httpCacheDirectory, 20 * 1024 * 1024);
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.interceptors().add(
            new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    String cacheHeaderValue = isOnline(context) 
                        ? "public, max-age=2419200" 
                        : "public, only-if-cached, max-stale=2419200" ;
                    Request request = originalRequest.newBuilder().build();
                    Response response = chain.proceed(request);
                    return response.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", cacheHeaderValue)
                        .build();
                }
            }
    );
    okHttpClient.networkInterceptors().add(
            new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    String cacheHeaderValue = isOnline(context) 
                        ? "public, max-age=2419200" 
                        : "public, only-if-cached, max-stale=2419200" ;
                    Request request = originalRequest.newBuilder().build();
                    Response response = chain.proceed(request);
                    return response.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", cacheHeaderValue)
                        .build();
                }
            }
    );
    return okHttpClient;
}

...

public interface RestDataResource {

    @GET("rest-data") 
    Call<List<RestItem>> getRestData();

}

6
看起来像您interceptors ()networkInterceptors ()并且完全相同。你为什么要重复这个?
toobsco42

不同类型的拦截器请阅读此处。github.com/square/okhttp/wiki/Interceptors
Rohit Bandil

是的,但是它们都做相同的事情,所以我很确定1个拦截器就足够了,对吗?
Ovidiu Latcu

22

我的解决方案:

private BackendService() {

    httpCacheDirectory = new File(context.getCacheDir(),  "responses");
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(httpCacheDirectory, cacheSize);

    httpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(REWRITE_RESPONSE_INTERCEPTOR)
            .addInterceptor(OFFLINE_INTERCEPTOR)
            .cache(cache)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.backend.com")
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    backendApi = retrofit.create(BackendApi.class);
}

private static final Interceptor REWRITE_RESPONSE_INTERCEPTOR = chain -> {
    Response originalResponse = chain.proceed(chain.request());
    String cacheControl = originalResponse.header("Cache-Control");

    if (cacheControl == null || cacheControl.contains("no-store") || cacheControl.contains("no-cache") ||
            cacheControl.contains("must-revalidate") || cacheControl.contains("max-age=0")) {
        return originalResponse.newBuilder()
                .header("Cache-Control", "public, max-age=" + 10)
                .build();
    } else {
        return originalResponse;
    }
};

private static final Interceptor OFFLINE_INTERCEPTOR = chain -> {
    Request request = chain.request();

    if (!isOnline()) {
        Log.d(TAG, "rewriting request");

        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);
};

public static boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) MyApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

3
不适用于我...获取504无法满足的请求(仅用于缓存)
zacharia

非常感谢您的解决方案。浪费2天向下滚动
ЯрославМовчан

1
是的,在我看来,这是唯一可行的解​​决方案。(改进版1.9.x + okHttp3)
Hoang Nguyen Huu

1
适用于翻新RETROFIT_VERSION = 2.2.0 OKHTTP_VERSION = 3.6.0
Tadas Valaitis

如何在此方法中添加builder.addheader()以访问具有授权的api?
Abhilash

6

答案是肯定的,基于上述答案,我开始编写单元测试以验证所有可能的用例:

  • 离线使用缓存
  • 首先使用缓存的响应,直到过期,然后使用网络
  • 首先使用网络,然后缓存一些请求
  • 不要在缓存中存储某些响应

我建立了一个小的帮助程序库来轻松配置OKHttp缓存,您可以在Github上查看相关的单元测试:https : //github.com/ncornette/OkCacheControl/blob/master/okcache-control/src/test/java/com/ ncornette / cache / OkCacheControlTest.java

演示脱机时如何使用缓存的单元测试:

@Test
public void test_USE_CACHE_WHEN_OFFLINE() throws Exception {
    //given
    givenResponseInCache("Expired Response in cache", -5, MINUTES);
    given(networkMonitor.isOnline()).willReturn(false);

    //when
    //This response is only used to not block when test fails
    mockWebServer.enqueue(new MockResponse().setResponseCode(404));
    Response response = getResponse();

    //then
    then(response.body().string()).isEqualTo("Expired Response in cache");
    then(cache.hitCount()).isEqualTo(1);
}

如您所见,即使缓存已过期也可以使用。希望会有所帮助。


2
你的lib很棒!感谢您的辛勤工作。图书馆
Hoang Nguyen Huu


2

使用Retrofit2和OkHTTP3进行缓存:

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 (NetworkUtils.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();

NetworkUtils.isNetworkAvailable()静态方法:

public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
    }

然后将客户端添加到改造生成器中:

Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

原始来源:https : //newfivefour.com/android-retrofit2-okhttp3-cache-network-request-offline.html


1
当我第一次使用离线模式加载时,它崩溃了!否则,它将无法正常工作
Zafer Celaloglu 2016年

这对我不起作用。在尝试整合它的原理后,我将其粘贴粘贴并尝试使用它,但是确实使它起作用。
男孩

App.sApp.getCacheDir()的作用是什么?
Huzaifa Asif
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.