美文网首页
Retrofit 源码解析

Retrofit 源码解析

作者: Android_大船 | 来源:发表于2019-03-03 21:07 被阅读0次

先说下 Retrofit 的简单使用方法。

先创建一个接口:

public interface GithubService {
    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);
}

然后是 Retrofit 的创建和使用:

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.github.com/")
                        .build();
GithubService githubService = retrofit.create(GithubService.class);
Call<List<Repo>> githubCall = githubService.listRepos("clipperl");
githubCall.enqueue(new Callback<List<Repo>>() {
    @Override
    public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
        System.out.println("success! " + response.body());
    }

    @Override
    public void onFailure(Call<List<Repo>> call, Throwable t) {

    }
});

我们先尝试从githubCall.enqueue()入手,结果发现进入了一个接口的抽象方法中:

public interface Call<T> extends Cloneable {
    void enqueue(Callback<T> callback);
}

其实真正的入口藏的比较深,我们要先从下边这行入手,:

GithubService githubService = retrofit.create(GithubService.class);

查看 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, @Nullable 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.adapt(okHttpCall);
          }
        });
  }

这里,关键的代码是Proxy.newProxyInstance(),这是一个动态代码,之前已经分析过动态代理在 Retrofit 中的使用,这里就不赘述了。

当我们调用自定义接口的抽象方法githubService.listRepos("clipperl")

Call<List<Repo>> githubCall = githubService.listRepos("clipperl");

其实执行的是InvocationHandlerinvoke(),所以可以得出一个结论:

githubCall 就是 serviceMethod.adapt(okHttpCall) 的返回值;

接着看serviceMethod.adapt(okHttpCall)

T adapt(Call<R> call) {
   return callAdapter.adapt(call);
}

再深入看,发现

public interface CallAdapter<R, T> {
    adapt(Call<R> call);
}

我们在ServiceMethod 类中搜索callAdapter,发现它在build()方法中被赋值了,

callAdapter = createCallAdapter();

继续找,

private CallAdapter<T, R> createCallAdapter() {
      Type returnType = method.getGenericReturnType();
      if (Utils.hasUnresolvableType(returnType)) {
        throw methodError(
            "Method return type must not include a type variable or wildcard: %s", returnType);
      }
      if (returnType == void.class) {
        throw methodError("Service methods cannot return void.");
      }
      Annotation[] annotations = method.getAnnotations();
      try {
        //noinspection unchecked
        return (CallAdapter<T, R>) retrofit.callAdapter(returnType, annotations);
      } catch (RuntimeException e) { // Wide exception range because factories are user code.
        throw methodError(e, "Unable to create call adapter for %s", returnType);
      }
    }

关键代码是:

return (CallAdapter<T, R>) retrofit.callAdapter(returnType, annotations);

接下来进入Retrofit类,

public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
    return nextCallAdapter(null, returnType, annotations);
}

再追踪,

public CallAdapter<?, ?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, Type returnType,
      Annotation[] annotations) {
    checkNotNull(returnType, "returnType == null");
    checkNotNull(annotations, "annotations == null");

    int start = callAdapterFactories.indexOf(skipPast) + 1;
    for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
      CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }

    StringBuilder builder = new StringBuilder("Could not locate call adapter for ")
        .append(returnType)
        .append(".\n");
    if (skipPast != null) {
      builder.append("  Skipped:");
      for (int i = 0; i < start; i++) {
        builder.append("\n   * ").append(callAdapterFactories.get(i).getClass().getName());
      }
      builder.append('\n');
    }
    builder.append("  Tried:");
    for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
      builder.append("\n   * ").append(callAdapterFactories.get(i).getClass().getName());
    }
    throw new IllegalArgumentException(builder.toString());
  }

关键代码是:

CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
if (adapter != null) {
    return adapter;
}

我们发现了一个新的变量callAdapterFactories

final List<CallAdapter.Factory> callAdapterFactories;

寻找它在哪被赋值,

public Retrofit build() {
      if (baseUrl == null) {
        throw new IllegalStateException("Base URL required.");
      }

      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }

      Executor callbackExecutor = 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 = new ArrayList<>(this.callAdapterFactories);
      callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));

      // Make a defensive copy of the converters.
      List<Converter.Factory> converterFactories =
          new ArrayList<>(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(new BuiltInConverters());
      converterFactories.addAll(this.converterFactories);

      return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
          unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
    }

关键代码是:

// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));

继续看

platform.defaultCallAdapterFactory(callbackExecutor)

进入Platform类找defaultCallAdapterFactory()方法,

直接找过去是:

CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
    if (callbackExecutor != null) {
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }
    return DefaultCallAdapterFactory.INSTANCE;
  }

其实我们用的是Platform的子类Android,长这个样子,

static class Android extends Platform {
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }

    @Override CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
      if (callbackExecutor == null) throw new AssertionError();
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }

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

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

我们看到 Android类中的defaultCallAdapterFactory()返回的是一个ExecutorCallAdapterFactory对象;

回忆一下,刚才我们一路在是寻找callAdapterFactories集合的元素的get()方法,

CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
if (adapter != null) {
    return adapter;
}

现在找到了,callAdapterFactories集合中的元素是ExecutorCallAdapterFactory对象,调用了ExecutorCallAdapterFactory对象的get()方法,接下看ExecutorCallAdapterFactory类的get()方法,

public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter<Object, Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }

      @Override public Call<Object> adapt(Call<Object> call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

get()方法返回了CallAdapter对象,这个CallAdapter对象就是前边一直要找的callAdapter

T adapt(Call<R> call) {
   return callAdapter.adapt(call);
}

调用callAdapteradapt()方法,创建一个ExecutorCallbackCall对象,这个ExecutorCallbackCall对象就是前边我们提到的githubCall

Call<List<Repo>> githubCall = githubService.listRepos("clipperl");

终于快到头了,githubCall调用的enqueue()方法,就是调用ExecutorCallbackCallenqueue()方法, ExecutorCallbackCallExecutorCallAdapterFactory的内部类,我们来看它的enqueeu()方法,

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) {
      checkNotNull(callback, "callback == null");

      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              if (delegate.isCanceled()) {
                // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
                callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
              } else {
                callback.onResponse(ExecutorCallbackCall.this, response);
              }
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              callback.onFailure(ExecutorCallbackCall.this, t);
            }
          });
        }
      });
    }
  }

enqueue(final Callback<T> callback)方法中传入的参数就是我们Retrofit请求成功后的回调接口,

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.github.com/")
                        .build();

GithubService githubService = retrofit.create(GithubService.class);
Call<List<Repo>> githubCall = githubService.listRepos("clipperl");
githubCall.enqueue(new Callback<List<Repo>>() {
    @Override
    public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
        System.out.println("success! " + response.body());
    }

    @Override
    public void onFailure(Call<List<Repo>> call, Throwable t) {

    }
});

只不过ExecutorCallbackCall会把我们的回调接口包装起来,

ExecutorCallbackCallenqueue()方法

@Override public void enqueue(final Callback<T> callback) {
      checkNotNull(callback, "callback == null");

      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              if (delegate.isCanceled()) {
                // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
                callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
              } else {
                callback.onResponse(ExecutorCallbackCall.this, response);
              }
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              callback.onFailure(ExecutorCallbackCall.this, t);
            }
          });
        }
      });
    }

我们的回调接口会被包到delegateenqueue()方法的Callback中,这是第二种Callback,请注意区分,后边还有第三种Callback,请别晕。

那么 delegate 是什么,它是我们从 ExecutorCallAdapterFactory 中传过来的,

final class ExecutorCallAdapterFactory extends CallAdapter.Factory {
  final Executor callbackExecutor;

  ExecutorCallAdapterFactory(Executor callbackExecutor) {
    this.callbackExecutor = callbackExecutor;
  }

  @Override
  public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter<Object, Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }

      @Override public Call<Object> adapt(Call<Object> call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

  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;
    }

还记得这个方法不,这个 call 就是 delegate

T adapt(Call<R> call) {
    return callAdapter.adapt(call);
}

那这个 call 是谁呢,它就是 okHttpCall,

ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.adapt(okHttpCall);

前边我们知道我们的 Callback 回调被这个 okHttpCallenqueue() 包起来了,我们去看看 okHttpCall

enqueue() 方法做了什么,

@Override public void enqueue(final Callback<T> callback) {
   checkNotNull(callback, "callback == null");

   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) {
       try {
         call = rawCall = createRawCall();
       } catch (Throwable t) {
         throwIfFatal(t);
         failure = creationFailure = t;
       }
     }
   }

   if (failure != null) {
     callback.onFailure(this, failure);
     return;
   }

   if (canceled) {
     call.cancel();
   }

   call.enqueue(new okhttp3.Callback() {
     @Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) {
       Response<T> response;
       try {
         response = parseResponse(rawResponse);
       } catch (Throwable e) {
         callFailure(e);
         return;
       }

       try {
         callback.onResponse(OkHttpCall.this, response);
       } catch (Throwable t) {
         t.printStackTrace();
       }
     }

     @Override public void onFailure(okhttp3.Call call, IOException e) {
       callFailure(e);
     }

     private void callFailure(Throwable e) {
       try {
         callback.onFailure(OkHttpCall.this, e);
       } catch (Throwable t) {
         t.printStackTrace();
       }
     }
   });
 }

这里创建了 okhttp3.Callcall,我们前边的 第二种 Callback 套的第一种 Callback (我们写的)这里又被第三种 Callback 包起来了,所以现在清楚了,RetrofitCallback 回调,被套了两层,最终是受 okhttp3Callback 调度。

接下来还有一个问题,异步的问题,Retrofit 使用 enqueue 异步请求,我们现在还没看到 Retrofit 怎么让我们的回调函数在主线程执行。

回到ExecutorCallAdapterFactory 的 内部类 ExecutorCallbackCallenqueue() 方法中,有这样一行代码,

callbackExecutor.execute(new Runnable()...

我们的回调接口原来在这里被放到一个线程中执行了,

final class ExecutorCallAdapterFactory extends CallAdapter.Factory {
  final Executor callbackExecutor;

  ExecutorCallAdapterFactory(Executor callbackExecutor) {
    this.callbackExecutor = callbackExecutor;
  }

  @Override
  public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter<Object, Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }

      @Override public Call<Object> adapt(Call<Object> call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

  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) {
      checkNotNull(callback, "callback == null");

      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              if (delegate.isCanceled()) {
                // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
                callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
              } else {
                callback.onResponse(ExecutorCallbackCall.this, response);
              }
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              callback.onFailure(ExecutorCallbackCall.this, t);
            }
          });
        }
      });
    }
  }
}

那接下来看下 callbackExecutor 是什么就能知道了,还记得之前那个 callAdapterFactories 吧,在给 赋值之前,callbackExecutor 就已经被赋值了,

public Retrofit build() {
      if (baseUrl == null) {
        throw new IllegalStateException("Base URL required.");
      }

      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }

      Executor callbackExecutor = 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 = new ArrayList<>(this.callAdapterFactories);
      callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));

      // Make a defensive copy of the converters.
      List<Converter.Factory> converterFactories =
          new ArrayList<>(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(new BuiltInConverters());
      converterFactories.addAll(this.converterFactories);

      return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
          unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
    }

是这几行代码,

Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
    callbackExecutor = platform.defaultCallbackExecutor();
}

我们还去找 Android 版的 Platform

static class Android extends Platform {
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }

    @Override CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
      if (callbackExecutor == null) throw new AssertionError();
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }

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

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

callbackExecutor 是一个 MainThreadExecutorMainThreadExecutorexecute() 就是用一个handler 向 UI 线程发消息,让我们的 Callback 回调代码在 UI 线程执行。

相关文章

网友评论

      本文标题:Retrofit 源码解析

      本文链接:https://www.haomeiwen.com/subject/cadmuqtx.html