//2.通过Retrofit中的方法client(this.retrofithuangcun())传入类名
//1.以下是我写好的类首先将其复制到你的代码中
public OkHttpClient retrofithuangcun(){
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/meCache";
File httpCacheDirectory =new File(path);
if (!httpCacheDirectory.exists()){
httpCacheDirectory.mkdir();
}
// File httpCacheDirectory = new File(context.getExternalCacheDir(), "responses");
//设置缓存 10M
int cacheSize =10 *1024 *1024;
final Cache cache =new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client =new OkHttpClient.Builder().cache(cache).addNetworkInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain)throws IOException {
Request request = chain.request();
okhttp3.Response proceed = chain.proceed(request);
proceed = proceed.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.addHeader("Cache-Control","max-age="+20000)
.build();
return proceed;
}
}).build();
return client;
}
网友评论