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

Android客户端调用POST API触发onFailure,Postman测试正常

问题排查与解决

1. 数据类型不匹配(核心问题)

Postman响应里的idTransaccion值为2371623456182,这个数值远超过Kotlin中Int类型的最大值(2^31-1 = 2147483647),反序列化时会直接抛出异常,导致Retrofit进入onFailure块。

解决:将idTransaccion的类型改为Long:

data class Response(
    @SerializedName("codigoRespuesta") val codigoRespuesta: String,
    @SerializedName("mensajeRespuesta") val mensajeRespuesta: String,
    @SerializedName("idTransaccion") val idTransaccion: Long
)

2. 注解拼写错误

你的数据类里用了@serializedName,但Gson/Moshi等序列化库要求的正确注解是首字母大写的@SerializedName,拼写错误会导致字段无法映射,同样引发反序列化失败。

3. 其他排查方向

如果上述修改后仍有问题,检查以下点:

  • 确认客户端请求的Header(包括token)和Postman完全一致,比如Content-Type是否为application/json
  • 检查Retrofit实例是否配置了正确的序列化转换器(比如GsonConverterFactory)
  • 打印onFailure的异常信息,通过Throwable.message或printStackTrace()定位具体错误:
    call.enqueue(object : Callback<Response> {
        override fun onFailure(call: Call<Response>, t: Throwable) {
            t.printStackTrace() // 打印详细错误日志
        }
        // ...
    })
    

内容的提问来源于stack exchange,提问作者Ángel Ugarte

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 13:24:51