New: Support suspend modifier on functions for Kotlin! This allows you to express the asynchrony of HTTP requests in an idiomatic fashion for the language.
1 2
@GET("users/{id}") suspendfunuser(@Path("id") id: Long): User
Behind the scenes this behaves as if defined as fun user(…): Call and then invoked with Call.enqueue. You can also return Response for access to the response metadata.
现在你只需要在你的网络请求方法前添加 suspend 修饰符,即可畅享协程带来的便利。在幕后实现方式与上边我们提到的方式大同小异,返回值可以是反序列化后的对象,也可以是 Response < T > ,以方便我们访问响应元数据。