Retrofit 什么时候会像 Rx 一样正式添加对协程的调用适配器工厂支持?
When will retrofit add call adapter factory support for coroutine just like Rx officially?
我已经使用 Retrofit 工作了大约 3 年,自从引入 RxAndroid 以来,square up 添加了一个转换器工厂来简化对 RxStream 的响应。现在我看不到协程的任何官方补充。虽然我找到了一个由 Jake Wharton 开发的带有转换器的实验套件,但我真的怀疑它在实际项目中的可靠性。
有什么想法吗?
实施'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'
根据 this 文章,Retrofit 已经支持协程。没有吗?为了找到它,您不能花 5 秒钟而不是在这里写一个问题吗?!
只需在您的 Retrofit 界面中创建挂起方法并直接 return 您的数据对象。
@GET("api/todo")
suspend fun getTodo(@Path(value = "id") todoId: Int): Todo
在 link 阅读更多内容。
在 Retrofit 2.6.0 中添加了协程支持
在app.gradle
def retrofitVersion = '2.6.0'
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
API 服务接口
@GET("api/?")
suspend fun getImages(
@Query("key") apiKey: String,
@Query("q") query: String,
@Query("image_type") imageType: String,
@Query("pretty") yesOrNo: Boolean
):ApiResponse
这不是正确的方法,只是一个示例
fun getImagesForPicassoView() {
CoroutineScope(Dispatchers.IO).launch {
apiResponse.postValue(apiService.getImages(Constants.API_KEY,"food","photo",true))
}
}
目前 Retrofit 内置了挂起功能,因此您不再需要 return 将其作为 Deffered。只是 return 真实的元数据。像这样
@GET("/users")
suspend fun getUsers(): List<Users>
或者,您想在 https://s.id/kotlin-add
阅读我关于此的介绍
我已经使用 Retrofit 工作了大约 3 年,自从引入 RxAndroid 以来,square up 添加了一个转换器工厂来简化对 RxStream 的响应。现在我看不到协程的任何官方补充。虽然我找到了一个由 Jake Wharton 开发的带有转换器的实验套件,但我真的怀疑它在实际项目中的可靠性。
有什么想法吗?
实施'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'
根据 this 文章,Retrofit 已经支持协程。没有吗?为了找到它,您不能花 5 秒钟而不是在这里写一个问题吗?!
只需在您的 Retrofit 界面中创建挂起方法并直接 return 您的数据对象。
@GET("api/todo")
suspend fun getTodo(@Path(value = "id") todoId: Int): Todo
在 link 阅读更多内容。
在 Retrofit 2.6.0 中添加了协程支持
在app.gradle
def retrofitVersion = '2.6.0'
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
API 服务接口
@GET("api/?")
suspend fun getImages(
@Query("key") apiKey: String,
@Query("q") query: String,
@Query("image_type") imageType: String,
@Query("pretty") yesOrNo: Boolean
):ApiResponse
这不是正确的方法,只是一个示例
fun getImagesForPicassoView() {
CoroutineScope(Dispatchers.IO).launch {
apiResponse.postValue(apiService.getImages(Constants.API_KEY,"food","photo",true))
}
}
目前 Retrofit 内置了挂起功能,因此您不再需要 return 将其作为 Deffered。只是 return 真实的元数据。像这样
@GET("/users")
suspend fun getUsers(): List<Users>
或者,您想在 https://s.id/kotlin-add
阅读我关于此的介绍