publicRetrofitbuild(){// HTTP 请求的 host 路径if(baseUrl==null){thrownewIllegalStateException("Base URL required.");}// 用来执行请求的工厂,若没有设置就是使用 OKHttpClientokhttp3.Call.FactorycallFactory=this.callFactory;if(callFactory==null){callFactory=newOkHttpClient();}// 异步请求时回调线程切换的执行器ExecutorcallbackExecutor=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=newArrayList<>(this.callAdapterFactories);callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));// 数据转换器的工厂// Make a defensive copy of the converters.List<Converter.Factory>converterFactories=newArrayList<>(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(newBuiltInConverters());converterFactories.addAll(this.converterFactories);// 创建 Retrofit 实例returnnewRetrofit(callFactory,baseUrl,unmodifiableList(converterFactories),unmodifiableList(callAdapterFactories),callbackExecutor,validateEagerly);}
newInvocationHandler(){privatefinalPlatformplatform=Platform.get();@OverridepublicObjectinvoke(Objectproxy,Methodmethod,@NullableObject[]args)throwsThrowable{// If the method is a method from Object then defer to normal invocation.if(method.getDeclaringClass()==Object.class){returnmethod.invoke(this,args);}if(platform.isDefaultMethod(method)){returnplatform.invokeDefaultMethod(method,service,proxy,args);}ServiceMethod<Object,Object>serviceMethod=(ServiceMethod<Object,Object>)loadServiceMethod(method);OkHttpCall<Object>okHttpCall=newOkHttpCall<>(serviceMethod,args);returnserviceMethod.adapt(okHttpCall);}});
okhttp3.CalltoCall(@NullableObject...args)throwsIOException{// 用上了之前解析注解得到的结果RequestBuilderrequestBuilder=newRequestBuilder(httpMethod,baseUrl,relativeUrl,headers,contentType,hasBody,isFormEncoded,isMultipart);// 处理请求的参数@SuppressWarnings("unchecked")// It is an error to invoke a method with the wrong arg types.ParameterHandler<Object>[]handlers=(ParameterHandler<Object>[])parameterHandlers;intargumentCount=args!=null?args.length:0;if(argumentCount!=handlers.length){thrownewIllegalArgumentException("Argument count ("+argumentCount+") doesn't match expected count ("+handlers.length+")");}// 把注解和参数对应上for(intp=0;p<argumentCount;p++){handlers[p].apply(requestBuilder,args[p]);}// 最后还是通过 callFactory 来创建 Call returncallFactory.newCall(requestBuilder.build());}
delegate.enqueue(newCallback<T>(){@OverridepublicvoidonResponse(Call<T>call,finalResponse<T>response){// 切换线程callbackExecutor.execute(newRunnable(){@Overridepublicvoidrun(){if(delegate.isCanceled()){// Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.callback.onFailure(ExecutorCallbackCall.this,newIOException("Canceled"));}else{callback.onResponse(ExecutorCallbackCall.this,response);}}});}//...}