改造+ OkHttp +缓存+ HTTPS

时间:2014-09-17 09:39:04

标签: android caching https retrofit okhttp

我在将Retrofit与OkHttp Cache和HTTPS结合使用时遇到问题。简单地说,它不起作用。当我将端点切换到非https版本时,一切正常。

My Retrofit实例:

File cacheDirectory = new File(App.get().getApplicationContext()
    .getCacheDir().getAbsolutePath(), "HttpCache");

try {
    cache = new Cache(cacheDirectory, 10 * 1024 * 1024);
} catch (IOException ioe) {
    Log.w(TAG, ioe);
}

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setCache(cache);

mInstance = new RestAdapter.Builder()
    .setLogLevel(RestAdapter.LogLevel.HEADERS)
    .setEndpoint("https://....")
    .setClient(new OkClient(okHttpClient))
    .setConverter(new SimpleXMLConverter())
    .setExecutors(Executors.newSingleThreadExecutor(), new MainThreadExecutor())
    .setRequestInterceptor(new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Cache-Control", "public, max-age=900");
        }
    })
    .build()
    .create(Restu.class);

响应正确包含标题**Cache-Control: public, max-age=900**

0 个答案:

没有答案