Is Retrofit+Okhttp using httpCaching as a default in Android?

前端 未结 3 895
悲&欢浪女
悲&欢浪女 2020-12-28 11:25

I use retrofit and okhttp in one of our applications.

I can\'t really find a good explanation for the default behaviour of Retrofit.

If Okhttp is on the cl

3条回答
  •  长发绾君心
    2020-12-28 11:47

    DEPRECATED for OkHttpClient v2.0.0 and higher

    As Jesse Wilson pointed out you need to create your own cache.
    The following code should create a 10MB cache.

    File httpCacheDirectory = new File(application.getApplicationContext()
        .getCacheDir().getAbsolutePath(), "HttpCache");
    
    HttpResponseCache httpResponseCache = null;
    try {
       httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024);
    } catch (IOException e) {
       Log.e(getClass().getSimpleName(), "Could not create http cache", e);
    }
    
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setResponseCache(httpResponseCache);
    builder.setClient(new OkClient(okHttpClient));
    

    The code is based on Jesse Wilsons example on Github.

提交回复
热议问题