美文网首页网络
一些简单的拦截器interceptor

一些简单的拦截器interceptor

作者: Ad大成 | 来源:发表于2019-07-25 20:45 被阅读43次

    我们知道在项目中添加拦截器有俩种方法 一种是addInterceptor(interceptor)
    另一种是addNetworkInterceptor(interceptor)
    那么这两种拦截器有什么区别呢 前者是添加应用拦截器 后者是添加网络拦截器
    应用拦截器只管进出应用, 而具体里面的细节就需要网络拦截器来管理!比如一个
    接口有重定向,那么用network去添加才能显示重定向前后的地址和信息
    下面介绍简单的集中拦截器添加

    • 日志拦截器
       private void logInterceptorTest() {
            // OkHttp拦截器
            Interceptor interceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    // 请求及请求相关信息
                    Request request = chain.request(); // 核心代码:获取Request对象
                    HttpUrl url = request.url();
                    Headers headers = request.headers();
                    String method = request.method();
                    Log.i("Haoxueren", "intercept: " + url + "\n" + method + "\n" + headers);
                    // 响应及响应相关信息
                    Response response = chain.proceed(request); // 核心代码:继续向下执行网络请求
                    String message = response.message();
                    Headers responseHeaders = response.headers();
                    ResponseBody responseBody = response.peekBody(Integer.MAX_VALUE);
                    String string = responseBody.string();
                    Log.i("Haoxueren", "intercept: " + message + "\n" + string);
                    return response;
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .build();
            Request request = new Request.Builder()
                    .url("接口自己找吧")
                    .get()
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("Haoxueren", "onFailure: " + e.getMessage());
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.i("Haoxueren", "onResponse: " + response.body().string());
                }
            });
        }
    
    • 重定向拦截器(网络)
     private void networkInterceptorTest() {
            // OkHttp拦截器
            Interceptor interceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    // 请求及请求相关信息
                    Request request = chain.request(); // 核心代码:获取Request对象
                    HttpUrl url = request.url();
                    Headers headers = request.headers();
                    String method = request.method();
                    Log.i("Haoxueren", "intercept: " + url);
                    // 响应及响应相关信息
                    Response response = chain.proceed(request); // 核心代码:继续向下执行网络请求
                    String message = response.message();
                    Headers responseHeaders = response.headers();
                    ResponseBody responseBody = response.peekBody(Integer.MAX_VALUE);
                    String string = responseBody.string();
                    // Log.i("Haoxueren", "intercept: " + message + "\n" + string);
                    return response;
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    // .addInterceptor(interceptor)
                    .addNetworkInterceptor(interceptor)
                    .build();
            Request request = new Request.Builder()
                    .url("http://www.publicobject.com/helloworld.txt")
                    .header("className", "1901B")
                    .get()
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("Haoxueren", "onFailure: " + e.getMessage());
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.i("Haoxueren", "onResponse: " + response.body().string());
                }
            });
        }
    
    • 添加缓存的拦截器
     // 添加缓存
        private void addCacheHeaderTest() {
            File cacheDir = context.getCacheDir(); // 不需要读写SD卡权限
            Interceptor interceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    return chain.proceed(request).newBuilder()
                            .removeHeader("Pragma")
                            .header("Cache-Control", "max-age=3600")
                            .build();
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .cache(new Cache(cacheDir, 1024 * 1024 * 10)) // 10M
                    .addNetworkInterceptor(interceptor)
                    .build();
    
            Request request = new Request.Builder()
                    .url("https://gitee.com/Haoxueren/codes/5h3p0q4y8sa6rlme2kunw73/raw?blob_name=stars")
                    //.cacheControl(new CacheControl.Builder()..build())
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("Haoxueren", "onFailure: " + e.getMessage());
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.i("Haoxueren", "onResponse: " + response.body().string());
                }
            });
        }
    

    相关文章

      网友评论

        本文标题:一些简单的拦截器interceptor

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