前言
Retrofit是Jake Wharton 大神的作品,它被分为三部分分别是retrofit,adapter和convert,在源码对应3个module,本文是基于Retrofit2.2版本的,本文用到关键依赖moudle的build.gradle如下
compile "com.squareup.retrofit2:retrofit:${retrofit}"
compile "com.squareup.retrofit2:converter-gson:${retrofit}"
compile "com.squareup.retrofit2:converter-scalars:${retrofit}"
compile("com.squareup.retrofit2:converter-simplexml:${retrofit}")
compile "com.squareup.retrofit2:adapter-rxjava:${retrofit}"
compile "com.squareup.okhttp3:okhttp:${OKHTTP}"
gradle.properties
OKHTTP=3.6.0
retrofit = 2.2.0
PS: retrofit 默认就包含OkHttp的引用,这点可以在源码pom.xml看到有OkHttp的依赖
所以OkHttp的依赖可以不用加
结构图
本来还想自己画一下的,看到网上有人总结的比较好,这里就直接照搬了,感谢Stay
技术储备
1.注解
2.一些设计模式的了解:代理模式(动态代理),装饰模式,外观模式,适配器模式,策略模式
这一块网上资料很多,可以自己去查资料。另外插一句:设计模式这一块还是很重要的,以后进阶看源码以及自己写出高质量的代码都是需要这项硬技能
关键类简介
Retrofit
主要给开发者调用进行网络请求的类,负责把接口传入以及用户自定义的一些配置(解析配置,RxJava支持配置等)
- callFactory:调用工程,其实就是OKHttpClient的对象
- baseUrl:基础URL,用来拼上路径行程完整的URL
- converterFactories:转换器工厂集合,用来序列化和反序列化
- adapterFactories:适配器工厂集合,用来适配请求返回的类型
- callbackExecutor:请求方法回调时处理器
- validateEagerly:是否需要全部缓存申明的方法的标志位
ServiceMethod
对接口的方法解析成请求的Request
OkHttpCall
对网络请求的封装
如何识别注解(构造请求)
在Retrofit的create方法有句
ServiceMethod
跳转到方法ServiceMethod, ?> loadServiceMethod(Method method)
中可以看到Retrofit 169行ServiceMethod的构建方法result = new ServiceMethod.Builder<>(this, method).build();
跳转到build()函数可以看到
for (Annotation annotation : methodAnnotations) {
parseMethodAnnotation(annotation);
}
跳转到parseMethodAnnotation
方法可以看到我们熟悉的“GET”,"POST"
private void parseMethodAnnotation(Annotation annotation) {
if (annotation instanceof DELETE) {
parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
} else if (annotation instanceof GET) {
parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
} else if (annotation instanceof HEAD) {
parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
if (!Void.class.equals(responseType)) {
throw methodError("HEAD method must use Void as response type.");
}
}
······
}
这里是有关请求注解解析,另外的解析Header和请求地址方法拼接有相应方法Headers parseHeaders(String[] headers)
和parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody)
GitHubApi api = retrofit.create(GitHubApi.class)这里面用了什么设计模式,如何实现返回接口的对象
这里用到了动态代理的设计模式,方法如下
public T create(final Class 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, 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 serviceMethod =
(ServiceMethod) loadServiceMethod(method);
OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.callAdapter.adapt(okHttpCall);
}
});
}
有关动态代理的讲解可以参考这篇文章,讲的比较易懂,如果不懂,你可以先忽略,直接假使中间用了某种方式生成了接口GitHubApi.class的实现类及相应对象
Retrofit与OkHttp苟且的那些事
在Retrofit中,ServiceMethod
承载了一个Http请求的所有参数,OkHttpCall
为okhttp3.Call
的组合封装,生成用于OkHttp
所需的Request
以及okhttp3.Call
,交给OkHttp
去发送请求
说人话:
我们在创建Retrofit的时候是通过构造模式(Builder)
/*创建retrofit对象*/
Retrofit retrofit = new Retrofit.Builder()
.client(builder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(basePar.getBaseUrl())
.build();
其中client传递的就是OkHttpClient
,点击File->Find in Path
可以看出在ServiceMethod
和OkHttpCall
有关于okhttp3.Call
的引用,在类ExecutorCallAdapterFactory.ExecutorCallbackCall
利用代理器模式调用了execute()(同步请求)方法,enqueue()(异步请求),以同步请求为例关键代码如下
@Override public Response execute() throws IOException {
okhttp3.Call call;
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed.");
executed = true;
if (creationFailure != null) {
if (creationFailure instanceof IOException) {
throw (IOException) creationFailure;
} else {
throw (RuntimeException) creationFailure;
}
}
call = rawCall;
if (call == null) {
try {
call = rawCall = createRawCall();
} catch (IOException | RuntimeException e) {
creationFailure = e;
throw e;
}
}
}
if (canceled) {
call.cancel();
}
return parseResponse(call.execute());
}
接下来看createRawCall()方法
private okhttp3.Call createRawCall() throws IOException {
Request request = serviceMethod.toRequest(args);//并且网络请求,生成Request
okhttp3.Call call = serviceMethod.callFactory.newCall(request);//准备开始网络请求
if (call == null) {
throw new NullPointerException("Call.Factory returned null.");
}
return call;
}
这里就关联起与OkHttp的联系
Retrofit与RxJava的苟且的那些事
首先要build.gradle添加rxjava的依赖
RxJava与Retrofit结合的关键一句代码就是.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
,是的,只要这么一句代码就可以支持让接口的方法返回的Observable观察者对象,具体是怎么实现的呢
前面介绍了Retrofit的create()
方法中,最后一句代码是serviceMethod.callAdapter.adapt(okHttpCall);
这里的callAdapter
就是类RxJavaCallAdapter.java
,它实现自CallAdapter
,方法adapt(Call
代码如下
@Override public Object adapt(Call call) {
OnSubscribe> callFunc = isAsync
? new CallEnqueueOnSubscribe<>(call)
: new CallExecuteOnSubscribe<>(call);//根据同步请求和异步请求进行选择
OnSubscribe> func;
if (isResult) {
func = new ResultOnSubscribe<>(callFunc);
} else if (isBody) {
func = new BodyOnSubscribe<>(callFunc);
} else {
func = callFunc;
}
Observable> observable = Observable.create(func);
if (scheduler != null) {
observable = observable.subscribeOn(scheduler);
}
if (isSingle) {
return observable.toSingle();
}
if (isCompletable) {
return CompletableHelper.toCompletable(observable);
}
return observable;
}
其中CallExecuteOnSubscribe
和CallEnqueueOnSubscribe
继承自OnSubscribe
,对网络的请求发起在这里又进行了一次封装,这里以同步请求为例
final class CallExecuteOnSubscribe implements OnSubscribe> {
private final Call originalCall;
CallExecuteOnSubscribe(Call originalCall) {
this.originalCall = originalCall;
}
@Override public void call(Subscriber super Response> subscriber) {
// Since Call is a one-shot type, clone it for each new subscriber.
Call call = originalCall.clone();
CallArbiter arbiter = new CallArbiter<>(call, subscriber);
subscriber.add(arbiter);
subscriber.setProducer(arbiter);
Response response;
try {
response = call.execute();
} catch (Throwable t) {
Exceptions.throwIfFatal(t);
arbiter.emitError(t);
return;
}
arbiter.emitResponse(response);
}
}
还是很简单的,不对感觉少了点东西没讲,对了,怎么保证接口方法返回的是Observable
呢?
我们猜测返回Observable
与.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
方法必然是存在某种联系的,我们顺着这个思路发现使用Retrofit.java
类66行成员变量ListnextCallAdapter
有引用进入该方法
public CallAdapter, ?> nextCallAdapter(CallAdapter.Factory skipPast, Type returnType,
Annotation[] annotations) {
checkNotNull(returnType, "returnType == null");
checkNotNull(annotations, "annotations == null");
int start = adapterFactories.indexOf(skipPast) + 1;
for (int i = start, count = adapterFactories.size(); i < count; i++) {
CallAdapter, ?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
if (adapter != null) {
return adapter;
}
}
······
关键代码行CallAdapter, ?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
这里循环adapterFactories,检查接口的方法返回类型(returnType)与adapterFactories的某种函数关系之后是否相等,到此就验证了我们的猜想了。
PS: Retrofit 默认接口的函数返回的是Call
,在其源码,有默认 CallAdapter.Factory
的实现类DefaultCallAdapterFactory
与ExecutorCallAdapterFactory
参考
https://www.jianshu.com/p/45cb536be2f4
https://juejin.im/entry/58612b771b69e675fcd09e9
https://www.jianshu.com/p/6f6bb2f0ece9
https://richardcao.me/2016/05/29/Retrofit2%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%EF%BC%88%E4%B8%80%EF%BC%89/