Retrofit源码解析
Posted 流云易采
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Retrofit源码解析相关的知识,希望对你有一定的参考价值。
square公司开源了一系列的优秀库,比如Retrofit,OkHttp,Picasso等,
前面简单分析了Picasso的源码,这里来分析下Retrofit的使用:
一、gradle添加依赖
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
盗两张网上的图:
下面是从创建Retrofit出发,具体的使用流程;Retrofit最终的请求操作都是交由okHttp去执行的,执行的结果返回Response,再根据转换器进行解析成相对应的返回类型T;
Retrofit中使用了动态代理,方便了使用;通过retrofit.create返回的其实是个动态代理类,所有具体的处理逻辑交由MethodHandler进行处理;
下面是Retrofit系统中的整个类图,有点像外观模式,Retrofit持有所有子系统的引用;Retrofit比较重要的是两个Factory,一个使用来生成CallAdapter的CallAdapterFactory;一个是用来转换结果的ConvertFactory;这两个都可以用户自己进行添加。
在自定义的Service中,每一个method对应一个MethodHandler,MethodHandler持有retrofit,前面两个Factory以及生成Request的RequestFactory;在okHttp中,Request需要自己进行定义创建,而Retrofit简化了这个操作,进行了相应的封装,使用注解的方式来定义Request的相关参数信息;注解信息的解析则在RequestFactory中完成,通过RequestFactoryParser对注解信息进行简单解析,RequestBuilderAction是解析method中参数中的注解如@Path这些产生的中间产物,最终通过RequestBuilder来具体产生一个Request,RequestBuilder中持有okHttp中的Request.Builder类的引用,其创建Request过程其实都是交给okHttp来操作的;
生成的Request最终封装成为一个OkHttpCall,OkHttpCall则可以看做是对okHttp中Call的通过,它的enqueue等网络请求操作都是委托个给okHttp来操作的;同时对okHttp的返回Response进行解析,使用convertFactory,将其解析为用户所期望的返回类型;
二、使用
(一)使用Call形式
1、定义请求接口:
public interface RetrofirHttpService {
@GET("{user}")
Call<UserInfo> getData(@Path("user") String user);
}
注:UserInfo是自己定义的解析类:
public class UserInfo {
String username;
String password;
}
2、使用GET获取信息:
// 原始的CallBack方式
private void getUseCall() {
// 添加拦截器
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new LoggingInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://115.156.187.146/TransferServer/")
.client(client) // 添加okHttp
.addConverterFactory(GsonConverterFactory.create()) // GSON进行转换
.build();
RetrofirHttpService apiStores = retrofit.create(RetrofirHttpService.class);
Call<UserInfo> call = apiStores.getData("ServerMain.php");
// 异步调用
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Response<UserInfo> response, Retrofit retrofit) {
UserInfo data = response.body();
LogUtils.i("Call Result:" + data.m);
}
@Override
public void onFailure(Throwable t) {
LogUtils.e(t.toString());
}
});
}
(二)使用RxJava形式
1、定义请求接口
public interface RxHttpService {
@GET("{path}")
Observable<UserInfo> getData(@Path("path") String path);
}
2、具体使用
// 使用RxJava方式
private void getUseRxJava() {
// 添加拦截器
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new LoggingInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://115.156.187.146/TransferServer/")
.client(client) // 添加okHttp
.addConverterFactory(GsonConverterFactory.create()) // GSON进行转换
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RxHttpService apiStores = retrofit.create(RxHttpService.class);
Observable<UserInfo> observable = apiStores.getData("ServerMain.php");
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<UserInfo>() {
@Override
public void onNext(UserInfo user) {
LogUtils.i("Call Result:" + user.m);
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable error) {
LogUtils.e(error.toString());
}
});
}
三、源码分析:
(一)先来看创建动态代理类的过程:
RxHttpService apiStores = retrofit.create(RxHttpService.class);
动态代理的知识具体见《 设计模式汇总:结构型模型(上)》中的代理模式中的解释。
1)Retrofit#create:
/** Create an implementation of the API defined by the {@code service} interface. */
@SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety.
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, 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);
}
return loadMethodHandler(method).invoke(args);
}
});
}
标准的动态代理创建过程;
来看代理类和委托类之间的中间InvocationHandler类的invoke函数具体执行逻辑;
loadMethodHandler(method).invoke(args);
2)Retrofit#loadMethodHandler:
private final Map<Method, MethodHandler<?>> methodHandlerCache = new LinkedHashMap<>();
MethodHandler<?> loadMethodHandler(Method method) {
MethodHandler<?> handler;
synchronized (methodHandlerCache) {
handler = methodHandlerCache.get(method);
if (handler == null) {
handler = MethodHandler.create(this, method);
methodHandlerCache.put(method, handler);
}
}
return handler;
}
Retrofit维护了一个method对应的Map,这里将method都封装成一个MethodHandler类(可以当作是委托类);调用代理类,其实就是调用MethodHandler的invoke;所以具体的实现逻辑都在MethodHandler中;
(二)MethodHanlder
1)MethodHandler类:
final class MethodHandler<T> {
// MethodHandler持有一个retrofit对象
private final Retrofit retrofit;
// 类似于Volley中的 Request ,包含了HTTP请求的Url、Header信息,MediaType、Method以及RequestAction数组
private final RequestFactory requestFactory;
// HTTP请求返回数据的类型
private final CallAdapter<T> callAdapter;
// 对返回数据进行转换的类型转换器
private final Converter<ResponseBody, T> responseConverter;
private MethodHandler(Retrofit retrofit, RequestFactory requestFactory,
CallAdapter<T> callAdapter, Converter<ResponseBody, T> responseConverter) {
this.retrofit = retrofit;
this.requestFactory = requestFactory;
this.callAdapter = callAdapter;
this.responseConverter = responseConverter;
}
// 每一次请求的最终具体调用的函数
Object invoke(Object... args) {
// 可以发现最终是调用callAdapter的adapt函数
// 并且将相应的请求事务封装成一个OkHttpCall类进行处理
return callAdapter.adapt(new OkHttpCall<>(retrofit, requestFactory, responseConverter, args));
}
// 调用该静态类创建一个MethodHandler实例
@SuppressWarnings("unchecked")
static MethodHandler<?> create(Retrofit retrofit, Method method) {
// 创建CallAdapter
CallAdapter<Object> callAdapter = (CallAdapter<Object>) createCallAdapter(method, retrofit);
// 根据callAdapter来获取相应的返回类型
Type responseType = callAdapter.responseType();
// 创建结果类型转换器
Converter<ResponseBody, Object> responseConverter =
(Converter<ResponseBody, Object>) createResponseConverter(method, retrofit, responseType);
// 创建RequestFactory
RequestFactory requestFactory = RequestFactoryParser.parse(method, responseType, retrofit);
return new MethodHandler<>(retrofit, requestFactory, callAdapter, responseConverter);
}
// 创建相应的CallAdapter
private static CallAdapter<?> createCallAdapter(Method method, Retrofit retrofit) {
// 这个method即为service中自定义的一个方法,一般返回都是泛型类
Type returnType = method.getGenericReturnType();
if (Utils.hasUnresolvableType(returnType)) {
throw Utils.methodError(method,
"Method return type must not include a type variable or wildcard: %s", returnType);
}
if (returnType == void.class) {
throw Utils.methodError(method, "Service methods cannot return void.");
}
// 获取Method上对应的注解即(GET这些)
Annotation[] annotations = method.getAnnotations();
try {
// 根据注解来获取相应的Adapter
return retrofit.callAdapter(returnType, annotations);
} catch (RuntimeException e) { // Wide exception range because factories are user code.
throw Utils.methodError(e, method, "Unable to create call adapter for %s", returnType);
}
}
// 最后也是调用retrofit来实现的;
private static Converter<ResponseBody, ?> createResponseConverter(Method method,
Retrofit retrofit, Type responseType) {
Annotation[] annotations = method.getAnnotations();
try {
return retrofit.responseConverter(responseType, annotations);
} catch (RuntimeException e) { // Wide exception range because factories are user code.
throw Utils.methodError(e, method, "Unable to create converter for %s", responseType);
}
}
}
MethodHandler是系统为定义的每一个Service中的method中创建的一个具体执行类,通过调用其invoke函数,来执行具体的请求的逻辑;
它主要包含四个变量:
Retrofit: MethodHandler持有一个retrofit对象
private final Retrofit retrofit;
RequestFactory: 类似于Volley中的 Request ,包含了HTTP请求的Url、Header信息,MediaType、Method以及RequestAction数组
private final RequestFactory requestFactory;
CallAdapter: HTTP请求返回数据的类型
private final CallAdapter<T> callAdapter;
ResponseConverter: 对返回数据进行转换的类型转换器
private final Converter<ResponseBody, T> responseConverter;
具体来看每个变量的创建;
2)Retrofit#callAdapter:
private final List<CallAdapter.Factory> adapterFactories;
public CallAdapter<?> callAdapter(Type returnType, Annotation[] annotations) {
return nextCallAdapter(null, returnType, annotations);
}
/**
* Returns the {@link CallAdapter} for {@code returnType} from the available {@linkplain
* #callAdapterFactories() factories} except {@code skipPast}.
*/
// 返回相关的可用的CallAdapter(除了指定跳过的skipPast)
public CallAdapter<?> nextCallAdapter(CallAdapter.Factory skipPast, Type returnType,
Annotation[] annotations) {
checkNotNull(returnType, "returnType == null");
checkNotNull(annotations, "annotations == null");
int start = adapterFactories.indexOf(skipPast) + 1;
// 遍历adapterFactories中所有的Factory
for (int i = start, count = adapterFactories.size(); i < count; i++) {
CallAdapter<?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
// 如果找到,则返回该Adapter
if (adapter != null) {
return adapter;
}
}
// 如果获取不到相应的CallAdapter,则抛出异常
StringBuilder builder = new StringBuilder("Could not locate call adapter for ")
.append(returnType)
.append(". Tried:");
for (int i = start, count = adapterFactories.size(); i < count; i++) {
builder.append("\\n * ").append(adapterFactories.get(i).getClass().getName());
}
if (skipPast != null) {
builder.append("\\nSkipped:");
for (int i = 0; i < start; i++) {
builder.append("\\n * ").append(adapterFactories.get(i).getClass().getName());
}
}
throw new IllegalArgumentException(builder.toString());
}
RetroFit中有一个adapterFactories保存了所有CallAdapter.Factory,通过遍历这些factory来获取相应符合的CallAdapter;
先来看adapterFactories都保存了哪些Factory;从Retrofit创建开始
(三)Retrofit的创建
private final OkHttpClient client;
private final BaseUrl baseUrl;
private final List<Converter.Factory> converterFactories;
private final List<CallAdapter.Factory> adapterFactories;
private final Executor callbackExecutor;
private final boolean validateEagerly;
private Retrofit(OkHttpClient client, BaseUrl baseUrl, List<Converter.Factory> converterFactories,
List<CallAdapter.Factory> adapterFactories, Executor callbackExecutor,
boolean validateEagerly) {
this.client = client;
this.baseUrl = baseUrl;
this.converterFactories = converterFactories;
this.adapterFactories = adapterFactories;
this.callbackExecutor = callbackExecutor;
this.validateEagerly = validateEagerly;
}
/** Create the {@link Retrofit} instances. */
public Retrofit build() {
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
// 默认使用OkhttpClient
OkHttpClient client = this.client;
if (client == null) {
client = new OkHttpClient();
}
// 向adapterFactories添加了一个默认的CallAdapterFactory
// 如果前面add了CallAdpterfactory(如RxJavaCallAdapterFactory),则可以看到默认的Factory会添加到用户指定的Factory后面
// 则在遍历的时候,会优先遍历用户指定的Factory
List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(Platform.get().defaultCallAdapterFactory(callbackExecutor));
// Make a defensive copy of the converters.
List<Converter.Factory> converterFactories = new ArrayList<>(this.converterFactories);
return new Retrofit(client, baseUrl, converterFactories, adapterFactories, callbackExecutor,
validateEagerly);
}
/**
* 用户添加自定义的CallAdapterFactory
*/
public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
adapterFactories.add(checkNotNull(factory, "factory == null"));
return this;
}
用户可以根据自己需要来添加自定义的CallAdapterFactory,这里来看系统默认的CallAdapterFactory;
Platform.get()是根据系统不同来创建不同的运行环境,比如是再Android上还是Java上运行时由区别的,如下所示:
1)Platform:
class Platform {
private static final Platform PLATFORM = findPlatform();
static Platform get() {
return PLATFORM;
}
private static Platform findPlatform() {
// 如果是Android系统
try {
Class.forName("android.os.Build");
if (Build.VERSION.SDK_INT != 0) {
return new Android();
}
} catch (ClassNotFoundException ignored) {
}
// 如果是Java系统
try {
Class.forName("java.util.Optional");
return new Java8();
} catch (ClassNotFoundException ignored) {
}
// 如果是其他系统
return new Platform();
}
static class Android extends Platform {
// 创建默认的CallAdapterFactory过程
@Override CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
if (callbackExecutor == null) {
// 如果没有指定callbackExecutor,或者为null,则新创建一个MainThreadExecutor
callbackExecutor = new MainThreadExecutor();
}
return new ExecutorCallAdapterFactory(callbackExecutor);
}
// 该Executor的主要工作逻辑就是调用主线程的Looper来创建一个对应的Handler
// 执行execute就是操作Handler进行post事件(Runnable)
static class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override public void execute(Runnable r) {
handler.post(r);
}
}
}
}
可以看到对应不同的系统平台,Retrofit提供了不同的Platform版本; 默认的CallAdapterFactory是个ExecutorCallAdapterFactory,它是通过callbackExecutor来创建的;
如果用户没有执行相应的callbackExecutor,系统或创建一个默认的MainThreadExecutor,该MainThreadExecutor的主要执行逻辑就是使用主线程的Handler来post Runnable消息;
然后Retrofit把ExecutorCallAdapterFactory实例添加到adapterFactories中;
2)继续前面MethodHandler的查找CallAdapter的逻辑:
CallAdapter<?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
final class ExecutorCallAdapterFactory implements CallAdapter.Factory {
private final Executor callbackExecutor;
ExecutorCallAdapterFactory(Executor callbackExecutor) {
this.callbackExecutor = callbackExecutor;
}
@Override
public CallAdapter<Call<?>> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
// 根据returnType获得具体的返回类型
if (Utils.getRawType(returnType) != Call.class) {
return null;
}
final Type responseType = Utils.getCallResponseType(returnType);
// 返回一个CallAdapter
return new CallAdapter<Call<?>>() {
@Override
public Type responseType() {
return responseType;
}
// 调用adapt返回一个ExecutorCallbackCall类
@Override
public <R> Call<R> adapt(Call<R> call) {
return new ExecutorCallbackCall<>(callbackExecutor, call);
}
};
}
将前面所有的逻辑串在一起,使用动态代理创建的代理类使用的InvokeHandler,调用其invoke来实现一个具体的方法调用;委托类是系统自定义创建的MethodHandler,系统根据method来创建相应的MethodHandler;MethodHandler中持有对CallAdapter的引用,CallAdapter是通过相应的CallAdpterFactory来创建的,如果加入了自定义的比如RxJava,最终会添加到Retrofit中的一个factory链表中,系统通过扫描链表来获取合适的CallAdpterFactory来创建CallAdapter;
如果没有自定义CallAdapterFactory,则会默认生成一个CallAdapterFactory;通过这个默认的Factory会创建一个默认的CallAdapter;
代理类的方法实现,是通过InvokeHandler的invoke来实现的;在系统定义的Invokehandler中进而调用MethodHandler的invoke函数,该invoke函数会继续调用CallAdpter的adapt,由上知,最终返回一个ExecutorCallbackCall类;
继续来看ExecutorCallbackCall类:
3)ExecutorCallbackCall类:
final class ExecutorCallAdapterFactory implements CallAdapter.Factory {
static final class ExecutorCallbackCall<T> implements Call<T> {
private final Executor callbackExecutor;
private final Call<T> delegate;
// 注意这里的Call传递进来的是一个封装好的OkHttpCall类
ExecutorCallbackCall(Executor callbackExecutor, Call<T> delegate) {
this.callbackExecutor = callbackExecutor;
this.delegate = delegate;
}
@Override public void enqueue(Callback<T> callback) {
delegate.enqueue(new ExecutorCallback<>(callbackExecutor, callback));
}
@Override public Response<T> execute() throws IOException {
return delegate.execute();
}
@Override public void cancel() {
delegate.cancel();
}
@SuppressWarnings("CloneDoesntCallSuperClone") // Performing deep clone.
@Override public Call<T> clone() {
return new ExecutorCallbackCall<>(callbackExecutor, delegate.clone());
}
}
static final class ExecutorCallback<T> implements Callback<T> {
private final Executor callbackExecutor;
private final Callback<T> delegate;
ExecutorCallback(Executor callbackExecutor, Callback<T> delegate) {
this.callbackExecutor = callbackExecutor;
this.delegate = delegate;
}
@Override public void onResponse(final Response<T> response, final Retrofit retrofit) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
delegate.onResponse(response, retrofit);
}
});
}
@Override public void onFailure(final Throwable t) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
delegate.onFailure(t);
}
});
}
}
}
可以看出来ExecutorCallbackCall也是一个代理类,它的委托类是OkHttpCall;所以使用默认的CallAdapterFactory返回的是一个封装了OkHttpCall的一个Call类,其具体的类型是ExecutorCallbackCall;
4)OkHttpCall:
final class OkHttpCall<T> implements Call<T> {
private final Retrofit retrofit;
private final RequestFactory requestFactory;
private final Converter<ResponseBody, T> responseConverter;
private final Object[] args;
private volatile com.squareup.okhttp.Call rawCall;
private boolean executed; // Guarded by this.
private volatile boolean canceled;
OkHttpCall(Retrofit retrofit, RequestFactory requestFactory,
Converter<ResponseBody, T> responseConverter, Object[] args) {
this.retrofit = retrofit;
this.requestFactory = requestFactory;
this.responseConverter = responseConverter;
this.args = args;
}
@SuppressWarnings("CloneDoesntCallSuperClone")
// We are a final type & this saves clearing state.
@Override
public OkHttpCall<T> clone() {
return new OkHttpCall<>(retrofit, requestFactory, responseConverter, args);
}
}
来看enqueue操作;在用户代码中:
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Response<UserInfo> response, Retrofit retrofit) {
UserInfo data = response.body();
LogUtils.i("Call Result:" + data.m);
}
@Override
public void onFailure(Throwable t) {
LogUtils.e(t.toString());
}
});
由前面知这里的Call对应的是一个ExecutorCallbackCall,来看起enqueue操作:
5)ExecutorCallbackCall#enqueue:
@Override
public void enqueue(Callback<T> callback) {
delegate.enqueue(new ExecutorCallback<>(callbackExecutor, callback));
}
delegate对应的即是OkHttpCall,先来看其enqueue传入的参数: ExecutorCallback
static final class ExecutorCallback<T> implements Callback<T> {
private final Executor callbackExecutor;
private final Callback<T> delegate;
ExecutorCallback(Executor callbackExecutor, Callback<T> delegate) {
this.callbackExecutor = callbackExecutor;
this.delegate = delegate;
}
@Override
public void onResponse(final Response<T> response, final Retrofit retrofit) {
callbackExecutor.execute(new Runnable() {
@Override
public void run() {
delegate.onResponse(response, retrofit);
}
});
}
@Override
public void onFailure(final Throwable t) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
delegate.onFailure(t);
}
});
}
}
ExecutorCallback也是一个代理类,用来封装用户定义的Callback,即类内部的变量delegate;callbackExecutor是查找Platform过程中new Android时创建的,其execute的逻辑较为简单,就是使用主线程对应的Handler来将Runnable事件post出去;可以看到,这里最终的响应都会在主线程中,而且调用用户自定义的onResponse和onFailure;
ExecutorCallback的主要作用就是封装Callback,将Callback的响应切换到主线程中;
6)继续来看对应的OkHttpCall的enqueue操作:
@Override
public void enqueue(final Callback<T> callback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed");
executed = true;
}
// 注意okHttp的call和retrofit的Call是不同
com.squareup.okhttp.Call rawCall;
try {
// 创建一个okhttp类型的Call
rawCall = createRawCall();
} catch (Throwable t) {
callback.onFailure(t);
return;
}
// 如果取消
if (canceled) {
rawCall.cancel();
}
this.rawCall = rawCall;
// 将Call中事务全部委托给okHttp来操作
// 调用okHttp的enqueue,使用okHttp的Callback来封装retrofit中的Callback
rawCall.enqueue(new com.squareup.okhttp.Callback() {
// 请求失败的响应
private void callFailure(Throwable e) {
try {
callback.onFailure(e);
} catch (Throwable t) {
t.printStackTrace();
}
}
// 请求成功的响应
private void callSuccess(Response<T> response) {
try {
callback.onResponse(response, retrofit);
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
public void onFailure(Request request, IOException e) {
callFailure(e);
}
@Override
public void onResponse(com.squareup.okhttp.Response rawResponse) {
Response<T> response;
try {
// 获得结果,并且解析结果
response = parseResponse(rawResponse);
} catch (Throwable e) {
callFailure(e);
return;
}
callSuccess(response);
}
});
}
OkHttpCall是对OkHttp里面的Call的封装,将所有的操作都委托给该Call进行操作;这里主要的是通过retrofit的call来创建一个okHttp的Call类;这里通过createRawCall来创建:
private com.squareup.okhttp.Call createRawCall() {
return retrofit.client().newCall(requestFactory.create(args));
}
这里是通过okHttpClient来创建的一个Call;首先来看 requestFactory是如何根据args参数来创建一个相应的Call的;
先来看requestFactory,它是MethodHandler中创建传递过来的;
RequestFactory requestFactory = RequestFactoryParser.parse(method, responseType, retrofit);
(四)RequestFactory:
final class RequestFactoryParser {
private final Method method;
static RequestFactory parse(Method method, Type responseType, Retrofit retrofit) {
RequestFactoryParser parser = new RequestFactoryParser(method);
// Method对应的注解这里解析
parser.parseMethodAnnotations(responseType);
parser.parseParameters(retrofit);
// 创建一个RequestFactory
return parser.toRequestFactory(retrofit.baseUrl());
}
private RequestFactoryParser(Method method) {
this.method = method;
}
}
RequestFactoryParser主要是根据method对应的注解及返回类型进行相应解析,得出相应的解析结果,然后创建一个RequestFactory来保存所有解析后结果,供创建OkHttpCall来使用。
<一>parseMethodAnnotations:
1)RequestFactoryParser#parseMethodAnnotations:
// 这里是对注解进行解析的核心代码
// 根据注解类型的不同(即请求类型类型的不同进行相应解析)
// 这里也对应的retrofit的基本用法
private void parseMethodAnnotations(Type responseType) {
for (Annotation annotation : method.getAnnotations()) {
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(method, "HEAD method must use Void as response type.");
}
} else if (annotation instanceof PATCH) {
parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
} else if (annotation instanceof POST) {
parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
} else if (annotation instanceof PUT) {
parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
} else if (annotation instanceof HTTP) {
HTTP http = (HTTP) annotation;
parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
} else if (annotation instanceof Headers) {
// 只有Headers是通过parseHeaders来解析的
String[] headersToParse = ((Headers) annotation).value();
if (headersToParse.length == 0) {
throw methodError(method, "@Headers annotation is empty.");
}
headers = parseHeaders(headersToParse);
} else if (annotation instanceof Multipart) {
// 可以看到Multipart和FormUrlEncoded不能同时定义
if (isFormEncoded) {
throw methodError(method, "Only one encoding annotation is allowed.");
}
isMultipart = true;
} else if (annotation instanceof FormUrlEncoded) {
if (isMultipart) {
throw methodError(method, "Only one encoding annotation is allowed.");
}
isFormEncoded = true;
}
}
if (httpMethod == null) {
throw methodError(method, "HTTP method annotation is required (e.g., @GET, @POST, etc.).");
}
// 如果不存在实体,但是用户定义为isMultipart或者isFormEncoded类型,则会抛出异常
if (!hasBody) {
if (isMultipart) {
throw methodError(method,
"Multipart can only be specified on HTTP methods with request body (e.g., @POST).");
}
if (isFormEncoded) {
throw methodError(method,
"FormUrlEncoded can only be specified on HTTP methods with request body "
+ "(e.g., @POST).");
}
}
}
这里是对注解进行解析的核心代码 ,根据注解类型的不同(即请求类型类型的不同) 进行相应解析,这里也对应的retrofit的基本用法;
可以看到除了headers使用的parseheader进行解析外,其他都是通过 parseHttpMethodAndPath进行解析的;这里来重点分析下parseHttpMethodAndPath;
2)parseHttpMethodAndPath:
// 这个函数只是先做了一个前期判断
private void parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody) {
if (this.httpMethod != null) {
throw methodError(method, "Only one HTTP method is allowed. Found: %s and %s.",
this.httpMethod, httpMethod);
}
// 对应"GET","POST"等
this.httpMethod = httpMethod;
// 标注有没有实体
this.hasBody = hasBody;
// 注解值为空直接返回
if (value.isEmpty()) {
return;
}
// 判断URL是否已经有查询字符串query string
// Get the relative URL path and existing query string, if present.
int question = value.indexOf('?');
if (question != -1 && question < value.length() - 1) {
// 保证URL的查询字符串中没有{...}之类的字符
// Ensure the query string does not have any named parameters.
String queryParams = value.substring(question + 1);
Matcher queryParamMatcher = PARAM_URL_REGEX.matcher(queryParams);
if (queryParamMatcher.find()) {
throw methodError(method, "URL query string \\"%s\\" must not have replace block. "
+ "For dynamic query parameters use @Query.", queryParams);
}
}
// 具体的解析继续由parsePathParameters来完成
this.relativeUrl = value;
this.relativeUrlParamNames = parsePathParameters(value);
}
这个函数仅是做了一个简单的前期判断,将method对应的请求类型(httpMethod) ,注解值(relativeUrl),以及是否包含实体信息(hasBody)赋值给该RequestFactoryParser;
来看POST和GET的简单调用实例:
parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
继续来看解析函数parsePathParameters :
private static final String PARAM = "[a-zA-Z][a-zA-Z0-9_-]*";
private static final Pattern PARAM_NAME_REGEX = Pattern.compile(PARAM);
private static final Pattern PARAM_URL_REGEX = Pattern.compile("\\\\{(" + PARAM + ")\\\\}");
/**
* Gets the set of unique path parameters used in the given URI. If a parameter is used twice
* in the URI, it will only show up once in the set.
*/
// 检测出路径中所有{..}的字段,如{user}等,添加到一个Set中(因此不会重复添加)
static Set<String> parsePathParameters(String path) {
Matcher m = PARAM_URL_REGEX.matcher(path);
Set<String> patterns = new LinkedHashSet<>();
while (m.find()) {
patterns.add(m.group(1));
}
return patterns;
}
这一步仅是根据正则表达式获取URL中所有{…}类型的数据,添加到patterns这个Set中,然后返回给RequestFactoryParser,对relativeUrlParamNames进行赋值;
第一步解析完毕,来到第二步parseP
以上是关于Retrofit源码解析的主要内容,如果未能解决你的问题,请参考以下文章