【问题标题】:What is the reason for this error and how to fix it?此错误的原因是什么以及如何解决?
【发布时间】:2019-09-05 15:12:51
【问题描述】:

我将kotlin 用于带有databindingMVVM 模式的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?
)





【问题讨论】:

  • 发布完整的堆栈跟踪
  • 完整的堆栈跟踪是什么意思?
  • 在 LogCat 中,发布堆栈跟踪错误。
  • 是的,请给我一分钟。
  • 我不能全部发布!!!!!!!!!

标签: android api kotlin data-binding retrofit2


【解决方案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)

试试这个

【讨论】:

  • CoroutineCallAdapterFactory 类属于这个 gradle ->implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2' 只需确认你必须添加这个 gradle。如果您已经添加了这个 gradle,那么请描述您的错误?
  • 您的第一个错误已解决,对吗?对于延迟,您必须将 Response 更改为 Deferred> 供参考 -> @POST("userLogin") fun userLogin(@Query("userName") userName: String, @Query("userPassword")用户密码:字符串):响应
  • 您需要对每个错误进行完整描述。没有错误的细节对你没有帮助。
  • 现在如何从响应中获取我的用户甚至消息?
  • 你可以这样用吗? --> GlobalScope.launch { try { val response = service.userLogin("userName", "userPassword").await() withContext(Dispatchers.Main) { handleMovieResponse(response) } } catch (e: Exception) { e. printStackTrace() } }
【解决方案2】:

适用于稍后解决此问题的任何人。 如果您的改造服务方法没有暂停,就会发生这种情况。 使方法暂停,这个问题就解决了。

【讨论】:

    【解决方案3】:

    addCallAdapterFactory(CoroutineCallAdapterFactory())

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 2011-05-20
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多