Android—Retrofit解析
設計模式:
外觀模式,構建者模式,工廠模式,代理模式,適配器模式,策略模式,觀察者模式
Retrofit網絡通信八步驟
先看Create方法
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);}ServiceMethod<Object, Object> serviceMethod =(ServiceMethod<Object, Object>) loadServiceMethod(method);OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);return serviceMethod.callAdapter.adapt(okHttpCall);}}); }使用了動態代理,Proxy.newProxyInstance()返回一個代理類對象。
invoke方法接收三個參數,動態代理對象、我們調用的方法、參數數組;
loadServiceMethod方法:
ServiceMethod loadServiceMethod(Method method) {ServiceMethod result;synchronized (serviceMethodCache) {result = serviceMethodCache.get(method);if (result == null) {result = new ServiceMethod.Builder(this, method).build();serviceMethodCache.put(method, result);}}return result; }Build類:
public Builder(Retrofit retrofit, Method method) {this.retrofit = retrofit;this.method = method;// 獲取 method 中的所有注解this.methodAnnotations = method.getAnnotations();// 獲取 method 中方法的參數類型this.parameterTypes = method.getGenericParameterTypes();// 獲得參數的值this.parameterAnnotationsArray = method.getParameterAnnotations(); }build()方法主要內容
// 根據 retrofit 對象的 CallAdapterFactory 為 ServiceMethod 創建一個 callAdapter callAdapter = createCallAdapter(); // 根據 retrofit 對象創建一個 responseConverter,默認是一個 BuildInConveter responseConverter = createResponseConverter(); // 解析 method 的所有注解 for (Annotation annotation : methodAnnotations) {parseMethodAnnotation(annotation); }所以ServiceMethod主要封裝了callAdapter和responseConverter ,對注解進行了解析,對后面網絡請求做準備。
看回Create方法的代碼
return serviceMethod.callAdapter.adapt(okHttpCall);最后是調用了callAdapter的adapt方法,如果我們添加了RxJava2CallAdapter,
@Override public Object adapt(Call<R> call) {Observable<Response<R>> responseObservable = isAsync? new CallEnqueueObservable<>(call): new CallExecuteObservable<>(call);Observable<?> observable;if (isResult) {observable = new ResultObservable<>(responseObservable);} else if (isBody) {observable = new BodyObservable<>(responseObservable);} else {observable = responseObservable;}if (scheduler != null) {observable = observable.subscribeOn(scheduler);}if (isFlowable) {return observable.toFlowable(BackpressureStrategy.LATEST);}if (isSingle) {return observable.singleOrError();}if (isMaybe) {return observable.singleElement();}if (isCompletable) {return observable.ignoreElements();}return observable; }所以如果添加了RxJava2CallAdapter就返回observal對象。
Retrofit中還有一個Converter?,與CallAdapter一樣重要,用戶可以根據需求對這兩個進行自由擴展。
Okhttp中服務器返回的數據源就是ResponseBody對象。所以我們的目標就是對ResponseBody進行解析:
T convert(ResponseBody value){//do convert and return t }?Retrofit不僅可以對ResponseBody進行轉換,也可以對RequestBody進行轉換;具體其內部提供了一個Convert.Factory:
abstract class Factory {//對ResponseBody進行數據轉換的轉換器public @Nullable Converter<ResponseBody, ?> responseBodyConverter(Type type,Annotation[] annotations, Retrofit retrofit) {return null;}//將未知數據轉換成RequestBody的轉換器public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {return null;}//將未知數據轉換成String類型的數據轉換器public @Nullable Converter<?, String> stringConverter(Type type, Annotation[] annotations,Retrofit retrofit) {return null;} }開頭構建Retrofit對象的
.addConverterFactory(GsonConverterFactory.create()) //添加Gson就是用來添加Gson對象的
public static GsonConverterFactory create() {return create(new Gson());}public static GsonConverterFactory create(Gson gson) {return new GsonConverterFactory(gson);}class GsonConverterFactory extends Converter.Factory{//持有一個gson對象用來講原始數據轉換成JavaBeanprivate final Gson gson;private GsonConverterFactory(Gson gson) {this.gson = gson;}}具體轉換過程可以看Gson原理分析。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Android—Retrofit解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dropbox推独立应用,公司估值已达1
- 下一篇: android sina oauth2.