gpt4 book ai didi

android - 改造 2.6.0 异常 : java. lang.IllegalArgumentException : Unable to create call adapter for kotlinx. coroutines.Deferred

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:18:13 27 4
gpt4 key购买 nike

我有一个使用 Kotlin 协程和 Retrofit 的项目。

我有这些依赖项:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'

今天我把项目中的Retrofit更新到了2.6.0。在 https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter据记载,它现在已被弃用。在 https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05据记载,Retrofit 目前支持suspend

因此,我删除了 retrofit2-kotlin-coroutines-adapter:0.9.2 并在 Retrofit 客户端中更改了这些行:

        retrofit = Retrofit.Builder()
.baseUrl(SERVER_URL)
.client(okHttpClient)
.addConverterFactory(MyGsonFactory.create(gson))
//.addCallAdapterFactory(CoroutineCallAdapterFactory()) - removed it.
.build()

运行时,第一个请求捕获异常:

java.lang.IllegalArgumentException: Unable to create call adapter for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>
for method Api.getUserInfo

据我所知,我可以使用 CallAdapter.Factory() 而不是 CoroutineCallAdapterFactory(),但它是抽象的。

如果在 Api 类中,我更改了一个在开头添加 suspend 的请求:

@FormUrlEncoded
@POST("user/info/")
suspend fun getUserInfo(@Field("token") token: String): Deferred<UserInfoResponse>

override suspend fun getUserInfo(token: String): Deferred<UserInfoResponse> =
service.getUserInfo(token)

我得到这个异常:

java.lang.RuntimeException: Unable to invoke no-args constructor for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>. Registering an InstanceCreator with Gson for this type may fix this problem.

最佳答案

阅读 https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05我看到了:

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.

@GET("users/{id}") suspend fun user(@Path("id") long id): 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.

Currently this integration only supports non-null response body types. Follow issue 3075 for nullable type support.

我这样更改了请求:添加了 suspend 并删除了 Deferred:

@FormUrlEncoded
@POST("user/info/")
suspend fun getUserInfo(@Field("token") token: String): UserInfoResponse


override suspend fun getUserInfo(token: String): UserInfoResponse =
service.getUserInfo(token)

然后在交互器中(或者只是在调用方法 getUserInfo(token) 时)移除 await():

override suspend fun getUserInfo(token: String): UserInfoResponse =
// api.getUserInfo(token).await() - was before.
api.getUserInfo(token)

更新

有一次我遇到下载PDF文件需要去掉Api类中的suspend的情况。参见 How to download PDF file with Retrofit and Kotlin coroutines? .

关于android - 改造 2.6.0 异常 : java. lang.IllegalArgumentException : Unable to create call adapter for kotlinx. coroutines.Deferred,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473539/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com