最近时间比较空闲,梳理了下retrofit的源码,和大家分享下,估计分析的不是很完善,不喜勿喷
从整体架构上了说,retrofit是对okhttp的进一步封装,底层使用的是okhttp框架,通过动态代理的设计模式和注解,并且和rxjava进行无缝对接,使使用起来更加方便,准确的说retrofit只负责网络请求接口的封装。
1.1 Retrofit的创建
public Retrofit build() {
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
// Make a defensive copy of the converters.
List<Converter.Factory> converterFactories =
new ArrayList<>(1 + this.converterFactories.size());
// Add the built-in converter factory first. This prevents overriding its behavior but also
// ensures correct behavior when using converters that consume all types.
converterFactories.add(new BuiltInConverters());
converterFactories.addAll(this.converterFactories);
return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
}
Retrofit(okhttp3.Call.Factory callFactory, HttpUrl baseUrl,
List<Converter.Factory> converterFactories, List<CallAdapter.Factory> callAdapterFactories,
@Nullable Executor callbackExecutor, boolean validateEagerly) {
this.callFactory = callFactory;
this.baseUrl = baseUrl;
this.converterFactories = converterFactories; // Copy+unmodifiable at call site.
this.callAdapterFactories = callAdapterFactories; // Copy+unmodifiable at call site.
this.callbackExecutor = callbackExecutor;
this.validateEagerly = validateEagerly;
}
通过源码可以看出Retrofit通过builder的方式进行创建。
baseUrl-- 网络请求的URL地址
callFactory-- 生产网络请求器,Retrofit使用的也是Okhttp的网络请求,只是对Okhttp进行了封装
converterFactories -- 数据工厂转换器类,主要是对数据进行转换
callAdapterFactories -- 网络请求适配器工厂
callbackExecutor -- 回调方法执行器
比如 -- .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create());可以和RXJava进行无缝对接
1.2 最重要的一步就是进行网络请求
public <T> T create(final Class<T> service) {
Utils.validateServiceInterface(service);
if (validateEagerly) {
eagerlyValidateMethods(service);
}
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
new InvocationHandler() {
private final Platform platform = Platform.get();
@Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
// If the method is a method from Object then defer to normal invocation.
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
ServiceMethod<Object, Object> serviceMethod =
(ServiceMethod<Object, Object>) loadServiceMethod(method);
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.adapt(okHttpCall);
}
});
}
下面这三行是最重要的部分,retrofit通过动态代理的设计方式将Okhttp进行了封装,而真正的请求是通过Okhttp进行的
ServiceMethod<Object, Object> serviceMethod = (ServiceMethod<Object, Object>) loadServiceMethod(method);
//Retroit的请求主要是通过一个接口进行的,比如
@PUT("platform/client/password/resetPassword")
Call<BaseResponse> forgetPassword(@Body ForgetPasswordRequest request);
serviceMethod的作用就是将接口里面的注解进行解析,得到请求的方式,请求的URL地址等一系列请求的条件
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
//这一步的作用是构建一个真正的请求对象,Retrofit的所有的请求都是通过OklhttpCall进行实现的。
return serviceMethod.adapt(okHttpCall);
//通过这一步返回一个OkhttpCall进行代理包装
来源:CSDN
作者:qq_36190583
链接:https://blog.csdn.net/qq_36190583/article/details/103850652