【发布时间】:2021-11-22 13:42:32
【问题描述】:
每当我尝试使用 Retrofit 创建请求时,它都会失败。奇怪的是它之前工作过,我没有改变任何关于代码的东西。现在几个月后它不再起作用了。我收到以下错误:IllegalArgumentException: Unable to create @Body converter for class apis.Config (parameter #2)
我尝试从 Gson 更改为 Moshi,但并没有解决问题。我还搜索了 Stackoverflow,发现人们也有类似的问题,但解决方案似乎对我不起作用,因为它们主要是关于重复的 SerializedNames,而我的代码中并非如此。
API 服务
interface ApiService {
@Headers(
"Content-Type: application/json"
)
@POST("api")
fun getActivityInvite(@Path("voiceChannelId") voiceChannelId: String, @Body body: Config, @Header("Authorization") authorization: String): Call<ActivityInvite>
}
配置类
data class Config(@SerializedName("target_application_id") val targetApplicationId: String){
@SerializedName("max_age")
val maxAge = 86400
@SerializedName("max_uses")
val maxUses = 0
@SerializedName("target_type")
val targetType = 2
@SerializedName("temporary")
val temporary = false
@SerializedName("validate")
val validate = null
}
API 类
class Api {
private val retrofit: Retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()
private val apiService: ApiService = retrofit.create(ApiService::class.java)
private val props: Properties = PropertiesUtil.getProperties("project.properties")
fun getActivityInvite(voiceId: String, activityId: String): String?{
val config = Config(activityId)
val request = apiService.getActivityInvite(voiceId, config, "Bot ${props.getProperty("bot_token")}")
val response = request.execute()
return response.body()?.getInviteUrl()
}
}
【问题讨论】:
标签: java json kotlin gson retrofit