为什么我的响应主体为空,状态为 200?

Why is my response body null, status 200?

我正在尝试从此 url:http:"//192.168.0.220:8000/records/?account_id=2"

获取响应正文

在 android 工作室中,我获得状态 200,但正文始终为空。谁能告诉我我做错了什么?

邮递员的响应如下所示:

{
    "result": [
        {
            "id": 1,
            "account_id": 2,
            "title": "ez",
            "datetime": "2021-03-21T00:00:00",
            "description": "ez2",
            "image": null,
            "recording": null
        },
        {
            "id": 2,
            "account_id": 2,
            "title": "ez",
            "datetime": "2021-03-21T00:00:00",
            "description": "ez2",
            "image": null,
            "recording": null
        },
....

android 工作室中的回复:

I/System.out: Response{protocol=http/1.1, code=200, message=OK, url=http://192.168.0.220:8000/records/?account_id=2}
    Item(id=null, account_id=null, title=null, datetime=null, image=null, recording=null)

接口:

interface Gett {
    @GET("?account_id=2")
    fun getRecord(): Call<Record.Item>
}

Class:

class Record {
    data class Item(
        val id: String,
        val account_id: String,
        val title: String,
        val datetime: String,
        val image: String,
        val recording: String
    )
}

主要活动:

val retrofit = Retrofit.Builder()
                    .baseUrl("http://192.168.0.220:8000/records/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()


val service = retrofit.create(Gett::class.java)
            val call = service.getRecord()

call.enqueue(object : retrofit2.Callback<Record.Item> {
                override fun onResponse(call: Call<Record.Item>, response: Response<Record.Item>) {
                    if (response.code() == 200) {
                        println(response)
                        println(response.body()!!)
                    }
                }
                override fun onFailure(call: Call<Record.Item>, t: Throwable) {
                    println("fail")
                }
            })

可能是这个问题

interface Gett {
    @GET("?account_id=2")
    fun getRecord(): Call<Record.Item> <----
}

所以,改变你的模型

data class Record (
    val result: List<Item>
)

data class Item(
        val id: String,
        val account_id: String,
        val title: String,
        val datetime: String,
        val image: String,
        val recording: String
    )

我看到你的JSON有一个你的Item数组,所以把它改成。

interface Gett {
    @GET("?account_id=2")
    fun getRecord(): Call<Record>
}

谢谢@Kunn 指出 JSONObject.