OkHttp原理

/**

  • @CreateDate: 2019/12/16 9:51

  • OkHttp框架流程

  • 1、创建OkHttpClient对象

  • 2、封装request对象

  • 3、创建Call对象

  • 4、把request请求加入到调度器

  • 5、执行Call的网络请求,其中的责任链式的拦截器,也在这个步骤中实现,返回结果\

  • OkHttpClient对象

  •  OkHttpClient.newCall(Request request)创建Call
    
  • Request对象,封装请求头Map,请求方法GET/POST,url,RequestBody(Map<String,String>)

  • Response对象,code;//状态码,contentLength = -1;//返回包的长度, HashMap<>();//返回包的头信息, String body;//包的内容,isKeepAlive;//是否保持连接

  • Call对象 getResponse()方法调用拦截器链发起请求,构造AsyncCall、RealCall,调用分发器,发起请求

  •  Call enqueue(Callback callback)
    
  •      httpClient.getDispather().enqueue(new AsyncCall(callback));
    
  •  Call execute()
    
  •      return getResponse();
    
  • AsyncCall implements Runnable //执行InterceptorChain,发出请求,得到response

  •  run()
    
  •    Response response = getResponse();
    
  •      InterceptorChain interceptorChain = new InterceptorChain(interceptors,0,this,null);
    
  •      Response response = interceptorChain.proceed();
    
  •      callback.onResponse(Call.this,response);
    
  • Dispather对象 分发器,管理线程池,处理请求

  •  maxRequests         最多同时请求的数量
    
  •  maxRequestPreHost   同一个host最多允许请求的数量
    
  •  ExecutorService executorService
    
  •  enqueue(Call.AsyncCall asyncCall)
    
  •      execute(asyncCall);
    
  • InterceptorChain //配合Interceptor,逐一执行

  •  Call call  //Interceptor可以从这里获取请求、返回参数
    
  •  final List<Interceptor> interceptors;
    
  •  proceed()   //逐一执行Interceptor.intercept()
    
  •    Interceptor interceptor = interceptors.get(index);
    
  •    InterceptorChain next = new InterceptorChain(interceptors,index + 1,call, httpConnection);
    
  •    Response response = interceptor.intercept(next);
    
  • Interceptor

  •  intercept(next:InterceptorChain);
    
  • 1.重试拦截器 RetryInterceptor

  • 2.Http头拦截器 HeadersInterceptor

  • 3.选择有效socket连接的拦截器 ConnectionInterceptor

  • 4.socket通信拦截器 CallServiceInterceptor

  • ConnectionInterceptor

  • httpClient.getConnectionPool().getHttpConnection(httpUrl.getHost(),httpUrl.getPort());

  • CallServiceInterceptor

  •  InputStream inputStream = httpConnection.call(httpCodec);
    
  •  解析inputStream,return Response
    
  • ConnectionPool

  • private Deque httpConnections = new ArrayDeque<>();

  • //生成一个清理线程,这个线程会定期去检查,并且清理那些无用的连接,这里的无用是指没使用的间期超过了保留时间

  • HttpConnection getHttpConnection(String host,int port) //根据服务器地址与端口,来获取可复用的连接

  • HttpConnection

  •  call(HttpCodec httpCodec)
    
  •    createSocket();
    
  •     httpCodec.writeRequest(outputStream,request);
    
  •     return InputStream
    
  • HttpCodec

  •  writeRequest(OutputStream os,Request request)  拼接request数据流,写入到socket通道
    
  •  readLine(InputStream is)    读取服务器返回回来的一行数据
    
  •   readHeaders(InputStream is)    读取服务器返回的响应头
    
  •   ...
    
  • https://www.jianshu.com/p/2716977d2e20

  • Retrofit

  • interface Weather {
    @GET(“weatherApi”)
    Observable getWeather(@Query(“city”) String string);
    }

    动态代理,生成Weather代理,WeatherProxy
    Weather的具体实现: WeatherProxy.getWeather(),调用的InvocationHandler
    InvocationHandler解析getWeather()的注解,构造Request、解析返回类型(OkHttp.Call还是其它类型,其它类型的话还会使用adapter转换),并存储ConcurrentHashMap<Method, ServiceMethod> serviceMethodCache;
    最终调用okhttpClient.newCall(Request)

    请求的方式可以通过adapter改成rxjava,能够转成rxjava形式,是因为有同步方法execute,就是说不使用okhttp的异步方法

  • RxJava2CallAdapter okhttp适配Rxjava

  • Observable responseObservable = new CallObservable<>(call);

  • class CallObservable extends Observable{

  •  Override subscribeActual(Observer<? super T> observer)
    
  • }

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值