我正在使用 kotlin
for android 和 databinding
以及 MVVM
模式。
Retrofit2
发送和获取 api 调用。
当我发送登录调用时,我总是崩溃,这是消息:
java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.Object
我多次重新设计响应,它实际上与我的 api 的响应相匹配。 有什么帮助吗?
//my retrofit interface
interface RetrofitInterface {
@FormUrlEncoded
@POST("userLogin")
suspend fun userLogin(
@Field("userName") userName: String,
@Field("userPassword") userPassword: String
): Response<UserLoginResponse>
companion object {
operator fun invoke(
networkConnectionInterceptor: NetworkConnectionInterceptor
): RetrofitInterface {
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(networkConnectionInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://muUrlIsCorrect/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RetrofitInterface::class.java)
}
}
}
//My response class
data class UserLoginResponse(
var error: Boolean?,
var message: String?,
var user: User?
)
最佳答案
我想你忘了在 Retrofit.Builder() 中添加 .addCallAdapterFactory
请添加 .addCallAdapterFactory(CoroutineCallAdapterFactory())
如果您正在使用 coroutines-kotlin 否则您可以添加 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiInterface::class.java)
试试这个
关于android - 此错误的原因是什么以及如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808444/