在现代Android开发中,异步请求已经成为不可或缺的一部分。传统的异步请求往往涉及大量的回调逻辑,使代码难以维护和调试。随着Kotlin协程的引入,异步编程得到了极大的简化。而作为最流行的网络请求库之一,Retrofit早在Kotlin协程的早期就开始支持suspend函数的请求。本文将从源码角度,深度解析Retrofit如何实现对suspend函数的支持,并探讨这种支持带来的开发体验的提升。
一、Retrofit的演变:从回调到协程
-  传统的异步请求 
 在Kotlin协程出现之前,Retrofit通过回调机制处理异步请求。我们需要在请求方法中定义回调接口,Retrofit会在请求完成后调用回调函数。这种方式虽然解决了异步问题,但会导致"回调地狱"。
-  Kotlin协程的引入 
 Kotlin协程通过简洁的语法,将异步代码编写得如同同步代码一样。这种变革让我们能够更加轻松地处理复杂的异步逻辑。Retrofit也迅速跟进,增加了对suspend函数的支持,使得网络请求能够以一种更加直观的方式进行。
二、Retrofit如何支持suspend请求
 
2.1 Retrofit接口定义
开发者在使用Retrofit时,通常会定义一个接口,其中的方法会被Retrofit动态代理实现。自从支持协程以来,这些方法可以被声明为suspend函数。比如:
interface ApiService {
    @GET("users/{id}")
    suspend fun getUser(@Path("id") userId: String): User
}
2.2 suspend函数的实现原理
 
为了理解Retrofit如何支持suspend请求,我们需要从Kotlin协程的工作原理开始。Kotlin中的suspend函数会被编译器转换为一个带有Continuation参数的函数。这意味着在编译后,原本的suspend函数变成了以下形式:
public final Object getUser(String userId, Continuation<? super User> continuation) {
    // 内部实现
}
这个Continuation参数其实是一个回调接口,用于恢复协程的执行。它包含了resumeWith方法,用于在异步操作完成后继续执行协程。
 在Retrofit中,针对这种转换,Retrofit使用了自定义的CallAdapter来适配这种形式。接下来,我们会深入分析CallAdapter的源码。
2.3 CallAdapter与协程的结合
Retrofit的设计中,CallAdapter用于将底层的Call对象转换为用户需要的形式。在支持协程之前,CallAdapter主要负责将Call对象转换为同步或者异步的回调请求。而在协程支持引入后,Retrofit增加了对suspend函数的支持。
以下是CallAdapter接口的定义:
public interface CallAdapter<R, T> {
 /**
   * Returns the value type that this adapter uses when converting the HTTP response body to a Java
   * object. For example, the response type for {@code Call<Repo>} is {@code Repo}. This type is
   * used to prepare the {@code call} passed to {@code #adapt}.
   *
   * <p>Note: This is typically not the same type as the {@code returnType} provided to this call
   * adapter's factory.
   */
    Type responseType();
    /**
   * Returns an instance of {@code T} which delegates to {@code call}.
   *
   * <p>For example, given an instance for a hypothetical utility, {@code Async}, this instance
   * would return a new {@code Async<R>} which invoked {@code call} when run.
   *
   * <pre><code>
   * @Override
   * public <R> Async<R> adapt(final Call<R> call) {
   *   return Async.create(new Callable<Response<R>>() {
   *     @Override
   *     public Response<R> call() throws Exception {
   *       return call.execute();
   *     }
   *   });
   * }
   * </code></pre>
   */
    T adapt(Call<R> call);
}
为了支持suspend,Retrofit2内部引入了SuspendForBody,它是CallAdapter的一个组合器,继承自HttpServiceMethod,专门用于处理suspend函数请求。
static final class SuspendForBody<ResponseT> extends HttpServiceMethod<ResponseT, Object> {
    private final CallAdapter<ResponseT, Call<ResponseT>> callAdapter;
    // ....
    @Override
    protected Object adapt(Call<ResponseT> call, Object[] args) {
      call = callAdapter.adapt(call);
      //noinspection unchecked Checked by reflection inside RequestFactory.
      Continuation<ResponseT> continuation = (Continuation<ResponseT>) args[args.length - 1];
      // ...
      // 实际触发最终调用
      KotlinExtensions.await(call, continuation);
    }
  }
}
// KotlinExtensions
@JvmName("awaitNullable")
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) {
          continuation.resume(response.body())
        } else {
          continuation.resumeWithException(HttpException(response))
        }
      }
      override fun onFailure(call: Call<T?>, t: Throwable) {
        continuation.resumeWithException(t)
      }
    })
  }
}
上面的代码是SuspendForBody的核心逻辑。关键点如下:
-  suspendCancellableCoroutine:这是Kotlin提供的一个函数,用于将异步代码转换为协程代码。它允许你在协程中执行异步操作,并在操作完成后恢复协程。 
-  invokeOnCancellation:该函数用于在协程被取消时,取消网络请求,避免资源浪费。 
-  call.enqueue:Retrofit的网络请求是异步执行的,enqueue方法用于执行请求并在完成后调用回调。在回调中,成功时调用resume恢复协程,失败时调用resumeWithException抛出异常。 
2.4 还有一个问题,retrofit是如何判断是否为suspend函数呢?
如上文所言,suspend函数在编译后会加入Continuation对象作为参数,

// retrofit2.RequestFactory.Builder#parseParameter
 try {
     if (Utils.getRawType(parameterType) == Continuation.class) {
     // 如果有参数为Continuation 则判断为suspend函数
        this.isKotlinSuspendFunction = true;
        return null;
    }
} catch (NoClassDefFoundError e) {
}                   
2.4 异常处理与协程
在协程环境中,异常处理变得更加简洁直观。Retrofit的suspend支持允许开发者直接使用try-catch块来捕获请求中的异常,而无需像过去那样处理回调中的错误。
try {
    val user = apiService.getUser("123")
} catch (e: Exception) {
    // 处理异常
}
在这个场景中,如果请求失败,Retrofit内部会调用continuation.resumeWithException(t),然后在协程中抛出异常,最终被catch捕获。这使得异常处理与同步代码中的处理方式完全一致,极大地简化了异步代码的编写。
2.5 Retrofit源码中的调度器管理
虽然SuspendForBody负责将异步请求转换为协程形式,但协程的执行依赖于调度器。Retrofit的设计默认使用了OkHttp的内部线程池来管理请求的执行。然而,开发者可以通过自定义调度器来控制请求的执行上下文,从而避免主线程阻塞等问题。
Retrofit.Builder()
    .callbackExecutor(Dispatchers.IO.asExecutor())
    .build()
通过这样的设置,可以确保网络请求在后台线程池中执行,而不会阻塞主线程。
三、总结
从支持suspend的角度来看,Retrofit展示了其在现代Android开发中的灵活性与强大性。通过源码的解析,我们可以深入理解它是如何将Kotlin的协程特性融入其中,从而带来了更加简洁、直观的编程体验。对于我们开发者而言,充分利用协程与Retrofit的结合,能够显著提升代码的可读性和可维护性。



















