1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public interface RetrofitService { @GET ("query" ) Observable<PostInfo> getPostInfoRx (@Query("type" ) String type, @Query ("postid" ) String postid) ; } Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.kuaidi100.com/" ) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); RetrofitService service = retrofit.create(XxxInterface.class); Call<PostInfo> call = service.getPostInfo("yuantong" , "11111111111" ); call.enqueue(new Callback<PostInfo>() { @Override public void onResponse (Call<PostInfo> call, Response<PostInfo> response) { Log.i("http返回:" , response.body().toString() + "" ); } @Override public void onFailure (Call<PostInfo> call, Throwable t) { } });
上面是 Retrofit 的主要用法,主要重要的东西有 ConverterFactory , CallAdapterFactory , retrofit.create(XxxInterface.class) 三个东西
Retrofit.Builder() builder就是一个常见的建造者模式,没什么好说的。 有一个点需要注意一下1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public Retrofit build () { okhttp3.Call.Factory callFactory = this .callFactory; if (callFactory == null ) { callFactory = new OkHttpClient(); } Executor callbackExecutor = this .callbackExecutor; if (callbackExecutor == null ) { callbackExecutor = platform.defaultCallbackExecutor(); } List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this .callAdapterFactories); callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor)); List<Converter.Factory> converterFactories = new ArrayList<>( 1 + this .converterFactories.size() + platform.defaultConverterFactoriesSize()); converterFactories.add(new BuiltInConverters()); converterFactories.addAll(this .converterFactories); converterFactories.addAll(platform.defaultConverterFactories()); return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories), unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 final class BuiltInConverters extends Converter .Factory { @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { if (type == ResponseBody.class) { return Utils.isAnnotationPresent(annotations, Streaming.class) ? StreamingResponseBodyConverter.INSTANCE : BufferingResponseBodyConverter.INSTANCE; } if (type == Void.class) { return VoidResponseBodyConverter.INSTANCE; } return null ; } ... }
这里首先插入了一个 BuiltInConverters 内置的转换器。 当返回类型是 ResponseBody 的时候就会使用 BuiltInConverters ,返回 ResponseBody 就和 okhttp 的返回一致了。
Retrofit的重要部分
responseConverter : response的转换器 GsonConverterFactory 将string类型的返回值转换为javabean
callAdapter : call适配器。 DefaultCallAdapterFactory 生产默认的适配器。 默认的行为是将子线程的请求callback切换到主线程。 RxJava2CallAdapterFactory 适配rxjava
retrofit.create(XxxInterface.class) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public <T> T create (final Class<T> service) { ... return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service }, new InvocationHandler() { private final Platform platform = Platform.get(); private final Object[] emptyArgs = new Object[0 ]; @Override public @Nullable Object invoke (Object proxy, Method method, @Nullable Object[] args) throws Throwable { ... return loadServiceMethod(method).invoke(args != null ? args : emptyArgs); } }); }
这里使用了动态代理,create方法返回的接口对象是一个动态代理的实例
loadServiceMethod 1 2 3 4 5 6 7 8 9 10 11 12 13 ServiceMethod<?> loadServiceMethod(Method method) { ServiceMethod<?> result = serviceMethodCache.get(method); if (result != null ) return result; synchronized (serviceMethodCache) { result = serviceMethodCache.get(method); if (result == null ) { result = ServiceMethod.parseAnnotations(this , method); serviceMethodCache.put(method, result); } } return result; }
缓存 + 解析Method
ServiceMethod.parseAnnotations 1 2 3 4 5 6 7 8 9 abstract class ServiceMethod <T > { static <T> ServiceMethod<T> parseAnnotations (Retrofit retrofit, Method method) { RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method); ... return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory); } abstract @Nullable T invoke (Object[] args) ; }
在抽象父类里先调用了 RequestFactory.parseAnnotations(retrofit, method);
去解析方法参数的注解。HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
里解析了返回类型,并匹配 CallAdapter 和 Converter 。。。抽象父类的静态方法区调用了子类的静态方法。。。有些迷惑
HttpServiceMethod.parseAnnotations RequestFactory.parseAnnotations 都是一些注解的处理,不说了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations ( Retrofit retrofit, Method method, RequestFactory requestFactory) { boolean isKotlinSuspendFunction = requestFactory.isKotlinSuspendFunction; boolean continuationWantsResponse = false ; boolean continuationBodyNullable = false ; Annotation[] annotations = method.getAnnotations(); Type adapterType; if (isKotlinSuspendFunction) { Type[] parameterTypes = method.getGenericParameterTypes(); Type responseType = Utils.getParameterLowerBound(0 , (ParameterizedType) parameterTypes[parameterTypes.length - 1 ]); if (getRawType(responseType) == Response.class && responseType instanceof ParameterizedType) { responseType = Utils.getParameterUpperBound(0 , (ParameterizedType) responseType); continuationWantsResponse = true ; } else { } adapterType = new Utils.ParameterizedTypeImpl(null , Call.class, responseType); annotations = SkipCallbackExecutorImpl.ensurePresent(annotations); } else { adapterType = method.getGenericReturnType(); } CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method, adapterType, annotations); Type responseType = callAdapter.responseType(); ... Converter<ResponseBody, ResponseT> responseConverter = createResponseConverter(retrofit, method, responseType); okhttp3.Call.Factory callFactory = retrofit.callFactory; if (!isKotlinSuspendFunction) { return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter); } else if (continuationWantsResponse) { return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForResponse<>(requestFactory, callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter); } else { return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForBody<>(requestFactory, callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter, continuationBodyNullable); } }
ServiceMethod.invode 1 2 3 4 5 6 7 8 9 @Override final @Nullable ReturnT invoke (Object[] args) { Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter); return adapt(call, args); } protected abstract @Nullable ReturnT adapt (Call<ResponseT> call, Object[] args) ;
OkHttpCall 实现了 Retrofit 的 call 到 OkHttpCall 的转换 adapt 最终会调用 CallAdapter 的 adapt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 @Override public void enqueue (final Callback<T> callback) { okhttp3.Call call; Throwable failure; synchronized (this ) { if (executed) throw new IllegalStateException("Already executed." ); executed = true ; call = rawCall; failure = creationFailure; if (call == null && failure == null ) { call = rawCall = createRawCall(); } } ... call.enqueue(new okhttp3.Callback() { @Override public void onResponse (okhttp3.Call call, okhttp3.Response rawResponse) { Response<T> response; response = parseResponse(rawResponse); callback.onResponse(OkHttpCall.this , response); } @Override public void onFailure (okhttp3.Call call, IOException e) { callback.onFailure(OkHttpCall.this , e); } }); }
可以看一下默认的 DefaultCallAdapterFactory 是怎么实现的1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 @Override public @Nullable CallAdapter<?, ?> get( Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Call.class) { return null ; } ... final Type responseType = Utils.getParameterUpperBound(0 , (ParameterizedType) returnType); final Executor executor = Utils.isAnnotationPresent(annotations, SkipCallbackExecutor.class) ? null : callbackExecutor; return new CallAdapter<Object, Call<?>>() { @Override public Type responseType () { return responseType; } @Override public Call<Object> adapt (Call<Object> call) { return executor == null ? call : new ExecutorCallbackCall<>(executor, call); } }; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 static final class ExecutorCallbackCall <T > implements Call <T > { final Executor callbackExecutor; final Call<T> delegate; ExecutorCallbackCall(Executor callbackExecutor, Call<T> delegate) { this .callbackExecutor = callbackExecutor; this .delegate = delegate; } @Override public void enqueue (final Callback<T> callback) { delegate.enqueue(new Callback<T>() { @Override public void onResponse (Call<T> call, final Response<T> response) { callbackExecutor.execute(new Runnable() { ... }); } @Override public void onFailure (Call<T> call, final Throwable t) { callbackExecutor.execute(new Runnable() { ... }); } }); } @Override public Response<T> execute () throws IOException { return delegate.execute(); }
enqueue 里的返回 onResponse 和 onFailure 都是在 callbackExecutor callbackExecutor 也是在 retrofit 构建时传入的, 默认是 Android.MainThreadExecutor
也就是说默认 enqueue 的 callback 默认是执行在主线程的
对kotlin协程的支持 动态代理的api接口的方法调用支持suspend协程,具体的方法是使用 suspendCancellableCoroutine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 suspend fun <T : Any> Call<T>.await(): T { return suspendCancellableCoroutine { continuation -> continuation.invokeOnCancellation { cancel() } enqueue(object : Callback<T> { override fun onResponse (call: Call<T>, response: Response<T>) { if (response.isSuccessful) { val body = response.body() if (body == null ) { ... continuation.resumeWithException(e) } else { continuation.resume(body) } } else { continuation.resumeWithException(HttpException(response)) } } override fun onFailure (call: Call<T>, t: Throwable) { continuation.resumeWithException(t) } }) } }