Android 为什么我的回复主体为空,状态为200?
我正在尝试从以下url获取响应正文:http://192.168.0.220:8000/records/?account\u id=2 在android studio中,我获得状态200,但body始终为空。谁能告诉我我做错了什么 《邮递员》中的回应如下:Android 为什么我的回复主体为空,状态为200?,android,kotlin,retrofit2,Android,Kotlin,Retrofit2,我正在尝试从以下url获取响应正文:http://192.168.0.220:8000/records/?account\u id=2 在android studio中,我获得状态200,但body始终为空。谁能告诉我我做错了什么 《邮递员》中的回应如下: { "result": [ { "id": 1, "account_id": 2, &
{
"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 studio中的响应:
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>
}
主要活动:
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")
}
})
val改装=改装.Builder()
.baseUrl(“http://192.168.0.220:8000/records/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service=reformation.create(Gett::class.java)
val call=service.getRecord()
call.enqueue(对象:2.Callback{
覆盖fun onResponse(调用:调用,响应:响应){
if(response.code()==200){
println(响应)
println(response.body()!!)
}
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
println(“失败”)
}
})
问题可能是这样的
interface Gett {
@GET("?account_id=2")
fun getRecord(): Call<Record.Item> <----
}
谢谢,@Kunn指出JSONObject。问题可能是这样的
interface Gett {
@GET("?account_id=2")
fun getRecord(): Call<Record.Item> <----
}
谢谢,@Kunn指出JSONObject。回复不是列表。它是一个JsonObject,响应不是列表。这是一个JsonObject。
fun getRecord():Response
fun getRecord():Response
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
)
interface Gett {
@GET("?account_id=2")
fun getRecord(): Call<Record>
}