/**
-
@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)
-
}
*/