我正在将kotlin
和databinding
模式的MVVM
用于android。
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?
)
答案 0 :(得分:2)
适用于以后因此问题而来的任何人。 如果您的改装服务方法未暂停,则会发生这种情况。 使方法暂停,此问题已解决。
答案 1 :(得分:1)
我认为您忘记在Retrofit.Builder()中添加.addCallAdapterFactory
如果您使用的是coroutines-kotlin,请添加.addCallAdapterFactory(CoroutineCallAdapterFactory())
,否则您可以添加.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiInterface::class.java)
尝试一下
答案 2 :(得分:0)
addCallAdapterFactory(CoroutineCallAdapterFactory())