我正在使用Retrofit - OkHttp 进行网络服务调用。
使用OkHttp的OkHttpClient.Builder
获取OkHttpClient
时,
缓存有参数。
见下面一行:
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cache(?);// This last parameter
如何设置此缓存参数?
答案 0 :(得分:2)
使用以下代码,我们可以为 OkHttpClient 设置应用程序级缓存目录:
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
// [app] is your Application class object.
// using [app.getCacheDir()], get app cache directory.
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
//DISK_CACHE_SIZE is constant for OkHttpClient cache directory size.
//private int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cache(cache);
OkHttpClient okHttpClient=builder.build();