我正在将kotlin
与databinding
模式的MVVM
用于android。Retrofit2
发送和获取api调用。
当我发送登录 call 时,我总是崩溃,这是消息:
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
如果您使用的是协程-kotlin,请添加.addCallAdapterFactory(CoroutineCallAdapterFactory())
,否则您可以添加.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiInterface::class.java)
试试这个