You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Retrofit无法创建Response<Object>调用适配器致API请求失败求助

问题分析与解决方案

错误核心原因

触发该异常是因为Retrofit无法找到适配retrofit2.Response<java.lang.Object>的调用适配器。从异常栈可见,当前项目仅配置了CompletableFutureCallAdapterFactory和DefaultCallAdapterFactory,但这两个适配器无法兼容你定义的ApiCalls.apiCallForCheckSponsorAvailable方法的返回类型或签名规则。

常见触发场景:

  • 接口方法返回Response<Object>,但未配置支持泛型、协程的调用适配器
  • 使用Kotlin协程但缺失Retrofit协程适配依赖
  • 接口方法返回类型不符合适配器要求

分步解决

1. 添加协程调用适配器依赖

如果项目使用Kotlin协程,必须补充Retrofit的协程适配库。在Module级别的build.gradle中添加依赖:

// Retrofit 协程调用适配器(适配kotlinx-coroutines)
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
// 或对应Retrofit 2.9.0+的官方适配库
implementation 'com.squareup.retrofit2:adapter-kotlin-coroutines:2.9.0'

2. 配置Retrofit实例时添加适配器

在创建Retrofit的代码块中,加入协程调用适配器:

val retrofit = Retrofit.Builder()
    .baseUrl(BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    // 注册协程调用适配器
    .addCallAdapterFactory(CoroutineCallAdapterFactory())
    .build()

3. 修正ApiCalls接口方法定义

避免直接使用Response<Object>,建议定义具体的数据实体类替代泛型Object,同时确保方法使用suspend修饰(协程场景):

interface ApiCalls {
    @GET("sponsor/check-available")
    suspend fun apiCallForCheckSponsorAvailable(): Response<SponsorCheckResponse>
}

// 对应接口返回的实体类
data class SponsorCheckResponse(
    val isAvailable: Boolean,
    val message: String
    // 其他接口返回字段
)

4. 排查兼容性问题

  • 确保Retrofit版本与协程适配器版本匹配
  • 检查是否误移除了其他必要的适配器配置
  • 确认接口方法的HTTP注解(@GET/@POST等)、参数配置无错误

内容的提问来源于stack exchange,提问作者vishal parmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 15:08:22