美文网首页
Retrofit缓存

Retrofit缓存

作者: Android_小菜鸡 | 来源:发表于2018-07-09 09:19 被阅读0次

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

    }

    相关文章

      网友评论

          本文标题:Retrofit缓存

          本文链接:https://www.haomeiwen.com/subject/btkzuftx.html