Android app开发中Retrofit框架的初步上手使用

2019-12-10 19:05:41王振洲

MainThreadExecutor

public final class MainThreadExecutor implements Executor {
 private final Handler handler = new Handler(Looper.getMainLooper());

 @Override public void execute(Runnable r) {
  handler.post(r);
 }
}

如果是Android,通过Handler将回调发送到主线程执行,如果非Android,直接同步执行。
Platform看完了,RestAdapter的成员初始化完成,就要看怎么通过RestAdapter.create生成我们定义的接口的实现类了

RestAdapter.create

 public <T> T create(Class<T> service) {
  Utils.validateServiceClass(service);
  return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
    new RestHandler(getMethodInfoCache(service)));
 }
 
 Map<Method, RestMethodInfo> getMethodInfoCache(Class<?> service) {
  synchronized (serviceMethodInfoCache) {
   Map<Method, RestMethodInfo> methodInfoCache = serviceMethodInfoCache.get(service);
   if (methodInfoCache == null) {
    methodInfoCache = new LinkedHashMap<Method, RestMethodInfo>();
    serviceMethodInfoCache.put(service, methodInfoCache);
   }
   return methodInfoCache;
  }
 }

使用了动态代理,InvocationHandler是RestHandler,RestHandler有一个参数,是Method->RestMethodInfo的映射,初始化时这个映射是空的。重点就是这两个了:RestHandler,RestMethodInfo,

@Override public Object invoke(Object proxy, Method method, final Object[] args)
  throws Throwable {
 // If the method is a method from Object then defer to normal invocation.
 if (method.getDeclaringClass() == Object.class) { // 1
  return method.invoke(this, args);
 }
 
 // Load or create the details cache for the current method.
 final RestMethodInfo methodInfo = getMethodInfo(methodDetailsCache, method); // 2
 
 if (methodInfo.isSynchronous) { // 3
  try {
   return invokeRequest(requestInterceptor, methodInfo, args);
  } catch (RetrofitError error) {
   Throwable newError = errorHandler.handleError(error);
   if (newError == null) {
    throw new IllegalStateException("Error handler returned null for wrapped exception.",
      error);
   }
   throw newError;
  }
 }
 
 if (httpExecutor == null || callbackExecutor == null) {
  throw new IllegalStateException("Asynchronous invocation requires calling setExecutors.");
 }
 
 // Apply the interceptor synchronously, recording the interception so we can replay it later.
 // This way we still defer argument serialization to the background thread.
 final RequestInterceptorTape interceptorTape = new RequestInterceptorTape();
 requestInterceptor.intercept(interceptorTape); // 4
 
 if (methodInfo.isObservable) { // 5
  if (rxSupport == null) {
   if (Platform.HAS_RX_JAVA) {
    rxSupport = new RxSupport(httpExecutor, errorHandler);
   } else {
    throw new IllegalStateException("Observable method found but no RxJava on classpath");
   }
  }
  
  return rxSupport.createRequestObservable(new Callable<ResponseWrapper>() {
   @Override public ResponseWrapper call() throws Exception {
    return (ResponseWrapper) invokeRequest(interceptorTape, methodInfo, args);
   }
  });
 }
 
 Callback<?> callback = (Callback<?>) args[args.length - 1]; // 6
 httpExecutor.execute(new CallbackRunnable(callback, callbackExecutor, errorHandler) {
  @Override public ResponseWrapper obtainResponse() {
   return (ResponseWrapper) invokeRequest(interceptorTape, methodInfo, args);
  }
 });
 
 return null; // Asynchronous methods should have return type of void.
}