美文网首页
OkHttp拦截器/缓存

OkHttp拦截器/缓存

作者: 小慧sir | 来源:发表于2019-07-13 14:45 被阅读0次

缓存 创建 一个类 extends Application 在 manifests 中 配置 name=""

public class Mappliction extends Application {

    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
}

日志 拦截器 创建 一个类implements Interceptor

public class Minsterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //获得请求
        Request request = chain.request();
        HttpUrl url = request.url();
        if (url != null) {
            System.out.println("我为拦截器" + url);
        }else {
            System.out.println("拦截失败");
        }

        //继续请求
        Response proceed = chain.proceed(request);
        //返回请求
        return proceed;
    }
}

创建 OkHttp

File filesDir = Mappliction.context.getFilesDir();
OkHttpClient client = new OkHttpClient.Builder()
//添加拦截器
.addInterceptor(new Minsterceptor())
//缓存
.cache(new Cache(filesDir, 1024 * 1024))
 .build();

相关文章

网友评论

      本文标题:OkHttp拦截器/缓存

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