简体   繁体   English

Android:获得响应“200 ok”但 Retrofit 失败

[英]Android : Getting response “200 ok” but Retrofit fails

I am posting JSON array with Retrofit2 but when i post it, i'm getting "200 Ok" response and retrofit's onFailed method being called.我正在使用 Retrofit2 发布 JSON 数组,但是当我发布它时,我得到“200 Ok”响应并调用改造的 onFailed 方法。 And it says "Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $"它说“预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 上是 STRING”

Do i successfully send the JSON to WebService?我是否成功将 JSON 发送到 WebService? If it's successful then why Retrofit fails?如果成功了,为什么 Retrofit 会失败? My JSON array like this:我的 JSON 数组是这样的:

[{"Date":"2019-01-01T00:00:00","Duration":"25","Type":1,"Number":"53201","id":0,"isDataSent":false,"Name":"test isim","phoneNumber":"5554242422"}]

I have test the JSON with PostMan.我已经用 PostMan 测试了 JSON。 It is fine.没事。

My Retrofit request like this:我的 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())
                }
            })

Is there an expert to help me with this?有没有专家可以帮助我解决这个问题?

My model class:我的 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
 }

My Retrofit Interface:我的 Retrofit 接口:

interface RetrofitService {
@POST("api")
fun postCallModel(@Body callLogModel: List<CallLogModel>) : Call<List<CallLogModel>>
   }

Your POST is correct and thats why you get 200. But problem is in your response from server.您的POST是正确的,这就是您获得 200 的原因。但问题在于您从服务器的响应中。 It expect a array but getting object , which leads this error.它期望一个array但得到object ,这会导致此错误。

I solved the problem with @CommonsWare's help.我在@CommonsWare 的帮助下解决了这个问题。 I am posting json but i have to handle the response that comes from web service.我正在发布 json 但我必须处理来自 web 服务的响应。 And web service returns String.并且 web 服务返回字符串。 So i have changed the return type of my postCallModel()所以我改变了我的 postCallModel() 的返回类型

Old return type was like this:旧的返回类型是这样的:

Call<List<CallLogModel>>

And i changed it to:我将其更改为:

Call<Any>

Now i don't have to worry about what type of response web service returns.现在我不必担心 web 服务返回什么类型的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM