我正在嘗試配置緩存,其中包含Retrofit 1.9.0
和OkHtttp 2.5.0
。Retrofit + RxJava無法緩存響應,疑似響應標頭
這裏是我爲我的RestAdapter
提供OkHttpClient
:
@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;
}
我沒有忘記setClient
上RestAdapter.Builder
。還確定,我實際上使用RestAdapter
的實例與此客戶端集合。
甚至檢查文件是否在「http」文件夾下創建。他們是。
但是經過我打開WIFI和重裝我的屏幕我結束了OnError
回調Observable
端點此消息:
retrofit.RetrofitError: failed to connect to /10.40.31.12 (port 8888) after 10000ms: connect failed: ENETUNREACH (Network is unreachable)
免責聲明:我也許應該提及的是,最終Observable
從5人組合,與flatMap
和zip
在途中。
[可以使用OKHttp改造時使用緩存數據可能的重複](http://stackoverflow.com/questions/23429046/can-retrofit-with-okhttp-use-cache-data-when-offline) – njzk2