美文网首页
OKHttp源码解析(二)

OKHttp源码解析(二)

作者: MIRROR1217 | 来源:发表于2019-07-11 17:08 被阅读0次

    上一章中我们讲了OKHttp的基本用法,这章我们看下OKHttp的新建对象OkHttpClient的建立过程。

    我们直接看下它的源码,OkHttpClient okHttpClient = new OkHttpClient.Builder().build()如下:

    Builder(OkHttpClient okHttpClient) {
          this.dispatcher = okHttpClient.dispatcher; //调度器
          this.proxy = okHttpClient.proxy;//代理
          this.protocols = okHttpClient.protocols; //协议
          this.connectionSpecs = okHttpClient.connectionSpecs;//传输层版本和连接协议
          this.interceptors.addAll(okHttpClient.interceptors);//拦截器
          this.networkInterceptors.addAll(okHttpClient.networkInterceptors);//网络拦截器
          this.eventListenerFactory = okHttpClient.eventListenerFactory;//OkHttp监听请求到结束所有过程的监听器
          this.proxySelector = okHttpClient.proxySelector;//代理选择器
          this.cookieJar = okHttpClient.cookieJar;//cookie
          this.internalCache = okHttpClient.internalCache;
          this.cache = okHttpClient.cache;//cache 缓存
          this.socketFactory = okHttpClient.socketFactory;//socket 工厂
          this.sslSocketFactory = okHttpClient.sslSocketFactory;//安全套层socket工厂 用于https
          this.certificateChainCleaner = okHttpClient.certificateChainCleaner;//验证确认响应书,适用HTTPS 请求连接的主机名
          this.hostnameVerifier = okHttpClient.hostnameVerifier;//主机名字确认
          this.certificatePinner = okHttpClient.certificatePinner;//证书链
          this.proxyAuthenticator = okHttpClient.proxyAuthenticator;//代理身份验证
          this.authenticator = okHttpClient.authenticator;//本地省份验证
          this.connectionPool = okHttpClient.connectionPool;//链接池 复用连接
          this.dns = okHttpClient.dns;//域名
          this.followSslRedirects = okHttpClient.followSslRedirects;//安全套接层重定向
          this.followRedirects = okHttpClient.followRedirects;//本地重定向
          this.retryOnConnectionFailure = okHttpClient.retryOnConnectionFailure;//重试连接失败
          this.connectTimeout = okHttpClient.connectTimeout;//连接超时
          this.readTimeout = okHttpClient.readTimeout;//读取超时
          this.writeTimeout = okHttpClient.writeTimeout;//写入超时
          this.pingInterval = okHttpClient.pingInterval//心跳连接时间
        }
    

    可以看到,初始化使用了建造者模式,初始化连接过程的信息,这里可能有些状态不清楚,但是我们暂时不必要太深究它,我们首先要研究OkHttp的核心模式(ps:有些童靴研究源码喜欢跑偏,当然我以前也是,在此提醒)。

    接下来我们继续往下看:

    Request request = new Request.Builder()
          .url(url)
          .build();
    

    这里是一个通常的请求,将请求的url设置进去,我们看下这里做了什么:

    Builder(Request request) {
          this.url = request.url;
          this.method = request.method;
          this.body = request.body;
          this.tag = request.tag;
          this.headers = request.headers.newBuilder();
        }
    

    可以看到,这里也是初始化一些信息,比如请求的url、信息携带头部和请求参数等,我们继续往下看:

            Call call = okHttpClient.newCall(request);
            Response response = null;
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    response = response;
                }
            });
    
            try {
                response = call.execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    可以看到,这里是返回结果了,包括同步的execute方法和异步的enqueue方法。实际上我们还可以发现,这里同步或者异步得到的结果都是通过Call返回的,那我们可以得出结论,请求的过程和服务器返回的结果都是在Call处理的,所以下面我们需要看下Call的处理过程。

    相关文章

      网友评论

          本文标题:OKHttp源码解析(二)

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