Retrofit2

Retrofit
- Map> serviceMethodCache = new ConcurrentHashMap<>();[将Method解析后映射为ServiceMethod]
- okhttp3.Call.Factory callFactory <默认为OkHttpClient,用以产生Call>
- HttpUrl baseUrl
- List converterFactories <从前往后遍历,使用返回非null的Converter.Factory>
- 默认 BuiltInConverters(), 用户自定义ConverterFactory列表,OptionalConverterFactory.INSTANCE
- List callAdapterFactories
- 同上,先遍历用户自定义CallAdapterFactory, 默认为DefaultCallAdapterFactory,参数为callbackExecutor,默认用以callback的线程切换到Main
- Executor callbackExecutor, 默认的DefaultCallAdapterFactory的线程池,用以切换callback到Main
- bool validateEagerly ,true:第一次创建interface时检验所有method, false:使用method时检验

create()@Retrofit:
    - Proxy.newProxyInstance()进行动态代理接口类, 调用loadServiceMethod(method).invoke()
    - loadServiceMethod()带缓存的method解析器,根据Method的注解配置,产生ServiceMethod
    - ServiceMethod的实现类的CallAdapter,调用createCallAdapter() -> callAdapter()@Retrofit -> 遍历CallAdapterFactories,获得合适的CallAdapter
    - 使用HttpServiceMethod$CallAdapter包裹上述获得的CallAdapter,并且有requestFactory【RequestFactory,负责配置Request】, callFactory[OkHttpClient],responseConverter[从Converter列表最早匹配]

- invoke()@ServiceMethod  实现在HttpServiceMethod  ,产生一个OkHttpCall(requestFactory, args, callFactory, responseConverter)
- adapter(call,args)@HttpServiceMethod$CallAdapter
-> adapter(call) @ 从callFatory获得的callAdapter,这里使用默认的DefaultCallAdapterFactory
- 这里的CallAdapter的adapter() 返回一个  ExecutorCallbaclCall(executor, call)[将response的callback丢到主线程];


- 随后用户调用enqueue()@ExecutorCallbackCall

- enqueue()@OkHttpCall[retrofit类]

- createRawCall(),使用callFactory【OkHttpClient】.newCall(requestFactory【RequestFactory】.create(args))产生一个okhttp3.Call
    - create(args)@requestFactory 生成一个Request, RequestFactory与Method关联的
    - newCall()@OkHttpClient 生成一个RealCall()对象,包含OkHttpClient, Request,
- 随后调用enqueue()@RawCall
- parseResponse()@OkHttpCall  将okhttp3.Response包裹为Retrofit2定义的Response
    - parseResponse()的过程中会用Converter进行转换再返回

---剩下的都是OkHttp进行网络请求的部分了

你可能感兴趣的:(Retrofit2)