Retrofit + RxJava无法caching响应,可疑响应头

我试图configurationcachingRetrofit 1.9.0OkHtttp 2.5.0

这里是我如何为OkHttpClient提供RestAdapter

 @Provides @Singleton public OkHttpClient provideOkHttpClient() { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); okHttpClient.setReadTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); okHttpClient.setWriteTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); File cacheDir = new File(context.getCacheDir(), "http"); final Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE_IN_BYTES); okHttpClient.setCache(cache); okHttpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(request); Response finalResponse = response.newBuilder() .header("Cache-Control", String.format("public, max-stale=%d", 604800)) .build(); Log.d("OkHttp", finalResponse.toString()); Log.d("OkHttp Headers", finalResponse.headers().toString()); return finalResponse; } }); return okHttpClient; } 

我没有忘记在RestAdapter.Builder上设置客户RestAdapter.Builder 。 还确定,我实际上使用RestAdapter实例与此客户端集。

甚至检查文件是否在“http”文件夹下创build。 他们是。

但是,当我打开WIFI并重新加载我的屏幕时,我终于在Observable端点的OnErrorcallback中出现这个消息:

 retrofit.RetrofitError: failed to connect to /10.40.31.12 (port 8888) after 10000ms: connect failed: ENETUNREACH (Network is unreachable) 

免责声明:我应该提到,最后的Observable是从其他5人,在flatMapzip的方式结合起来。

Solutions Collecting From Web of "Retrofit + RxJava无法caching响应,可疑响应头"

我想我有一个答案。 简短的一句话是:“如果服务器发送无caching头响应,则无法完成”。

如果你想要更长的一个,细节如下。

我做了一个比较2后端的示例应用程序。 我们称它们为后端A和后端B. A给了我麻烦,所以我决定检查B.

A返回CacheControl = "no-cache, no-transform, max-age=0"

B返回Cache-Control = „public"响应头

我为后端做了相同的设置,只是不同的url。

  private void buildApi() { Gson gson = new GsonBuilder().create(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS); File cacheDir = new File(getCacheDir(), "http"); final Cache cache = new Cache(cacheDir, 1000000 * 10); okHttpClient.setCache(cache); okHttpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.d("OkHttp REQUEST", request.toString()); Log.d("OkHttp REQUEST Headers", request.headers().toString()); Response response = chain.proceed(request); response = response.newBuilder() .header("Cache-Control", String.format("public, max-age=%d, max-stale=%d", 60, RESPONSE_CACHE_LIFESPAN_IN_SECONDS)) .build(); Log.d("OkHttp RESPONSE", response.toString()); Log.d("OkHttp RESPONSE Headers", response.headers().toString()); return response; } }); RestAdapter.Builder builder = new RestAdapter.Builder() .setConverter(new StringGsonConverter(gson)) .setClient(new OkClient(okHttpClient)) .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { if (isNetworkAvailable()) { request.addHeader("Cache-Control", "public, max-age=" + 60); } else { request.addHeader("Cache-Control", "public, only-if-cached, max-stale=" + RESPONSE_CACHE_LIFESPAN_IN_SECONDS); } } }); builder.setEndpoint("http://this.is.under.vpn.so.wont.work.anyway/api"); A_API = builder.build().create(AApi.class); builder.setEndpoint("http://collector-prod-server.elasticbeanstalk.com/api"); B_API = builder.build().create(BApi.class); } 

两个电话,然后禁用WiFi。 caching工作正常B,但抛出504 Unsatisfiable Request (only-if-cached)

在这种情况下,覆盖头文件似乎无济于事。

你应该重写你的Request而不是Response 。 作为参考,请参阅重写请求的文档。 注意你也可以使用CacheControl类来代替构build你自己的头文件。 你的拦截器应该看起来像 –

 okHttpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request cachedRequest = request.newBuilder() .cacheControl(new CacheControl.Builder() .maxStale(7, TimeUnit.DAYS) .build()) .build(); return chain.proceed(cachedRequest); } });