[英]Android : Getting response “200 ok” but Retrofit fails
我正在使用 Retrofit2 發布 JSON 數組,但是當我發布它時,我得到“200 Ok”響應並調用改造的 onFailed 方法。 它說“預期 BEGIN_ARRAY 但在第 1 行第 2 列路徑 $ 上是 STRING”
我是否成功將 JSON 發送到 WebService? 如果成功了,為什么 Retrofit 會失敗? 我的 JSON 數組是這樣的:
[{"Date":"2019-01-01T00:00:00","Duration":"25","Type":1,"Number":"53201","id":0,"isDataSent":false,"Name":"test isim","phoneNumber":"5554242422"}]
我已經用 PostMan 測試了 JSON。 沒事。
我的 Retrofit 請求如下:
RetrofitClient.getClient()
.postCallModel(list)
.enqueue(object : Callback<List<CallLogModel>>{
override fun onFailure(call: Call<List<CallLogModel>>, t: Throwable) {
Log.d(tag,"Retrofit Failed!")
Log.d(tag,call.toString())
Log.d(tag,t.message)
}
override fun onResponse(call: Call<List<CallLogModel>>, response: Response<List<CallLogModel>>
) {
Log.d(tag,"Retrofit Succeeded!")
Log.d(tag,response.message())
}
})
有沒有專家可以幫助我解決這個問題?
我的 model class:
@Entity(tableName = "supportCallLog")
data class CallLogModel(
@SerializedName("Name")
val userName: String,
@SerializedName("phoneNumber")
val userPhone: String,
@SerializedName("Number")
val customerPhone: String,
@SerializedName("Time")
val callDuration: String,
@SerializedName("Type")
val callType: Int,
@SerializedName("Date")
val callDate: String,
var isDataSent : Boolean
){
@PrimaryKey(autoGenerate = true)
var id : Int = 0
}
我的 Retrofit 接口:
interface RetrofitService {
@POST("api")
fun postCallModel(@Body callLogModel: List<CallLogModel>) : Call<List<CallLogModel>>
}
您的POST
是正確的,這就是您獲得 200 的原因。但問題在於您從服務器的響應中。 它期望一個array
但得到object
,這會導致此錯誤。
我在@CommonsWare 的幫助下解決了這個問題。 我正在發布 json 但我必須處理來自 web 服務的響應。 並且 web 服務返回字符串。 所以我改變了我的 postCallModel() 的返回類型
舊的返回類型是這樣的:
Call<List<CallLogModel>>
我將其更改為:
Call<Any>
現在我不必擔心 web 服務返回什么類型的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.