zoukankan      html  css  js  c++  java
  • OkHttp原理

    原文:https://www.songma.com/news/txtlist_i28215v.html

     

    使用

    OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
    Request request = new Request.Builder()
                  .url(url)//请求链接
                  .build();//创建Request对象
    Call call = client .newCall(request);//创建Call对象
     
    Response response = call.execute();//获取Response对象
     

    1、okhttp工作的大致流程

    1.1、整体流程

    (1)、当我们通过OkhttpClient创立一个okHttpClient 、Request 、Call,并发起同步或者异步请求时;

    (2)、okhttp会通过Dispatcher对我们所有的Call(RealCall实现类)进行统一管理,并通过execute()及enqueue()方法对同步或者异步请求进行执行
    (3)、execute()及enqueue()这两个方法会最终调用RealCall中的getResponseWithInterceptorChain()方法,从阻拦器链中获取返回结果;
    (4)、拦截器链中,依次通ApplicationInterceptor应用拦截器、RetryAndFollowUpInterceptor(重定向阻拦器)、BridgeInterceptor(桥接阻拦器)、CacheInterceptor(缓存阻拦器)、ConnectInterceptor(连接阻拦器)、NetwrokInterceptor(网络拦截器)、CallServerInterceptor(请求阻拦器)对请求依次处理,与服务的建立连接后,获取返回数据,再经过上述阻拦器依次解决后,最后将结果返回给调用方。


    提供两张图便于了解和记忆:

    okhttp整体流程1

    okhttp整体流程2

    1.2、各大阻拦器

    getResponseWithInterceptorChain方法,也是整个OkHttp实现责任链模式的核心。

    #RealCall
    fun getResponseWithInterceptorChain(): Response {
        //创建拦截器数组
        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(interceptors, transmitter, null, 0, originalRequest, this,
            client.connectTimeoutMillis, client.readTimeoutMillis, client.writeTimeoutMillis)
        ...
        try {
          //启动责任链
          val response = chain.proceed(originalRequest)
          ...
          return response
        } catch (e: IOException) {
          ...
        }
      }

    拦截器作用: 

    ApplicationInterceptor: 应用拦截器,通过addInterceptor添加,拿到的是原始请求,可以添加一些自定义header、通用参数、参数加密、网关接入等等。

    RetryAndFollowUpInterceptor:重试和重定向拦截器,处理错误重试和重定向

    BridgeInterceptor:桥接拦截器,主要工作是为请求添加cookie、添加固定的header,比如Host、Content-Length、Content-Type、User-Agent等等,然后保存响应结果的cookie,如果响应使用gzip压缩过,则还需要进行解压。

    CacheInterceptor:缓存拦截器,如果命中缓存则不会发起网络请求。

    ConnectInterceptor:连接拦截器,内部会维护一个连接池,负责连接复用、创建连接(三次握手等等)、释放连接以及创建连接上的socket流。

    NetworkInterceptors:网络拦截器,用户自定义,通常用于监控网络层的数据传输。

    CallServerInterceptor:网络请求拦截器,在前置准备工作完成后,真正发起了网络请求。

    至此,OkHttp的核心执行流程就结束了,是不是有种豁然开朗的感觉? 

    OkHttp的内部实现通过一个责任链模式完成,将网络请求的各个阶段封装到各个链条中,实现了各层的解耦。

  • 相关阅读:
    7人脸识别
    1图片视频文件操作基础
    3直方图与二值化,图像梯度
    6模板匹配(人脸匹配案例)
    基础习题
    碎片知识点整理
    详解:MySQL数据表损坏的正确修复方案
    前端开发:模块化 — 高效重构
    分享几个基于jQuery不错的前端相册展示插件代码
    程序员编程10年的心得和体会
  • 原文地址:https://www.cnblogs.com/wytiger/p/12981834.html
Copyright © 2011-2022 走看看