Retrofit + OkHttp + cache + HTTPS

2019-08-09 01:58发布

I have problem with Retrofit combine with OkHttp Cache and HTTPS. Simply, it doesn't work. When I switch endpoint to non-https version, everything works.

My Retrofit instance:

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

Response correctly contains header **Cache-Control: public, max-age=900**.

0条回答
登录 后发表回答