okhttp Interceptor

Interceptor介绍

okhttp的拦截器就是将整个请求网络的过程的每一步都封装在不同的Interceptor里,这样说可能有点绕,简单点说就是把一个List里的Interceptor都顺序执行一遍,那么整个网络请求过程就完成了

  @Throws(IOException::class)
  internal fun getResponseWithInterceptorChain(): Response {
    // Build a full stack of interceptors.
    val interceptors = mutableListOf<Interceptor>()
    interceptors += client.interceptors
    interceptors += RetryAndFollowUpInterceptor(client)
    interceptors += BridgeInterceptor(client.cookieJar)
    interceptors += CacheInterceptor(client.cache)
    interceptors += ConnectInterceptor
    if (!forWebSocket) {
      interceptors += client.networkInterceptors
    }
    interceptors += CallServerInterceptor(forWebSocket)

    val chain = RealInterceptorChain(
        call = this,
        interceptors = interceptors,
        index = 0,
        exchange = null,
        request = originalRequest,
        connectTimeoutMillis = client.connectTimeoutMillis,
        readTimeoutMillis = client.readTimeoutMillis,
        writeTimeoutMillis = client.writeTimeoutMillis
    )

    var calledNoMoreExchanges = false
    try {
      val response = chain.proceed(originalRequest)
      if (isCanceled()) {
        response.closeQuietly()
        throw IOException("Canceled")
      }
      return response
    } catch (e: IOException) {
      calledNoMoreExchanges = true
      throw noMoreExchanges(e) as Throwable
    } finally {
      if (!calledNoMoreExchanges) {
        noMoreExchanges(null)
      }
    }
  }

 

 

 

  • RetryAndFollowUpInterceptor:用于重定向和发生错误时重试
  • BridgeInterceptor:应用层与网络层的桥梁,从代码中看主要是为request添加请求头,为response去除响应头
  • CacheInterceptor:处理请求与响应缓存
  • ConnectInterceptor:与服务器建立连接
  • CallServerInterceptor:责任链中最后一个拦截器,用最终得到的request发送请求,将获取到response返回给前面的拦截器处理

这样application interceptornetwork interceptor的区别就很明显了,应用拦截器不会关注重定向和错误重试等操作,并且获取到的 requestresponse并不是网络层的。而网络拦截器则与之相反,如果发生了重定向和重试,那么会被调用多次,获取的requestresponse是完整的。

Application interceptors

  • 不用关注重定向、重试等中间响应
  • 只会调用一次
  • 只需关注应用发起请求的意图,不用关心okhttp为其添加的头部信息,比如 If-None-Match
  • 可以不调用Chain.proceed()不发送请求
  • 可以多次调用Chain.proceed()来重试请求

Network Interceptors

  • 可以操作重定向、重试等中间响应
  • 当返回缓存的响应时不会被调用
  • 可以获取到网络传输中真实的请求与响应
  • 可以访问带有请求的连接

Interceptor介绍

我们先看拦截器的接口定义:

public interface Interceptor {
    //拦截处理
    Response intercept(Chain chain) throws IOException;
    interface Chain {
        //获取请求的request
          Request request();
        //处理request获取response
        Response proceed(Request request) throws IOException;
    
        /**
         * Returns the connection the request will be executed on. This is only available in the chains
         * of network interceptors; for application interceptors this is always null.
         */
        @Nullable Connection connection();
    }
}

 

实例

比如我们现在有这样一个需求,为了保护用户远离甫田系,要将所有请求某竞价排名公司(baidu)的接口地址都直接替换成 www.soso.com

import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class KeepAwayBaiduInterceptor implements Interceptor {
    private static final String TAG = "KeepAwayBaiduInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Log.i(TAG, "intercept host: " + request.url().host());
        if (request.url().host().equals("www.baidu.com")) {
            //如果发现host属于该公司地址,就基于原来的request生成一个新的request,并使用新的url地址,这样之前request里的信息就都保留了
            Request newRequest = request.newBuilder().url("http://www.soso.com").build();
            return chain.proceed(newRequest);
        } else {
            return chain.proceed(request);
        }
    }
}

我们再看一下如何okhttp请求里使用这个自定义的Interceptor

  • okhttp发起请求代码
public void doRequest(){
    //创建okHttpClient对象
    OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new KeepAwayBaiduInterceptor())//在此处添加我们的拦截器
            .build();
    //创建一个该竞价排名公司的Request
    final Request request = new Request.Builder()
            .url("http://www.baidu.com")
            .build();

    Call call = mOkHttpClient.newCall(request);
    //请求加入调度
    call.enqueue(new Callback()
    {
        @Override
        public void onFailure(@NonNull Call call, @NonNull final IOException e) {
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException
        {
            final String htmlStr =  response.body().string();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText("response:"+htmlStr);

                }
            });
        }
    });
}

总结

okhttp的拦截器就是在intercept(Chain chain)的回调中对Request和Response进行修改,然后通过chain.proceed(request)调起下一个拦截器。在okhttp中,网络连接也是一个拦截器(CallServerInterceptor),他是最后一个被调用的,负责将request写入网络流中,并从网络流中读取服务器返回的信息写入Response中返回给客户端

okhttpinterceptor采用的是责任链模式,在这条责任链中其中,前面的interceptor根据自己的需求处理request对象,处理完之后将其交给下一个interceptor,也就是上面代码中的chain.proceed(request)方法,然后等待下一个拦截器返回一个response,再对返回的结果进行处理,最终给请求的发起者返回一个响应结果。
posted @ 2023-03-06 11:25  xiaowang_lj  阅读(49)  评论(0编辑  收藏  举报