Retrofit + OkHttp + cache + HTTPS

I have a problem with Retrofit combining with OkHttp Cache and HTTPS. It just doesn't work. When I switch the endpoint to a 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);

      

The response contains the header correctly **Cache-Control: public, max-age=900**

.

+1


source to share





All Articles