我是 Retrofit 的新手,我遇到了一个奇怪的问题。我收到以下错误 -->
预期为 BEGIN_ARRAY 但在第 1 行第 2 列路径为 BEGIN_OBJECT $
然而,当我使用 curl 命令到达端点时,我得到了一个 json 数组。
对于出于某种原因不想使用 JsonObject 作为响应的 Retrofit 来阅读整个内容的人来说,json 是不同的。它看起来像下面这样。我应该提到这是来自 Firebase 实时数据库对象
这是来自 Retrofit 的 JsonObject
{
"jobs": [
{
"job": {
"city": "Toronto",
"job_id": 1,
"owner_id_map": 1,
"status": 0,
"title": "500 Posters in Toronto"
},
"posters": [
{
"poster": {
"job_id_map": 2,
"latitude": 43.64526533333377,
"locationDescription": "Union Station",
"longitude": -79.38060468539248,
"poster_id": 1,
"qr_code": "code2",
"worker_id": 1
},
"worker": [
{
"workerName": "Noris",
"worker_id": 1
}
]
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.64295669900133,
"locationDescription": "Queen and Spadina",
"longitude": -79.39368099382459,
"poster_id": 2,
"qr_code": "code3",
"worker_id": 2
},
"worker": [
{
"workerName": "Jack",
"worker_id": 2
}
]
},
{
"poster": {
"job_id_map": 3,
"latitude": 43.76892443268843,
"locationDescription": "North York Civic Center",
"longitude": -79.41292484238735,
"poster_id": 3,
"qr_code": "code4",
"worker_id": 3
},
"worker": [
{
"workerName": "Nicky",
"worker_id": 3
}
]
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Montreal",
"job_id": 2,
"owner_id_map": 1,
"status": 1,
"title": "20 Posters in Montreal"
},
"posters": [],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Ottawa",
"job_id": 3,
"owner_id_map": 2,
"status": 2,
"title": "350 Posters in Ottawa"
},
"posters": [
{
"poster": {
"job_id_map": 4,
"latitude": 43.63904362027761,
"locationDescription": "Harbourfront",
"longitude": -79.38193214610098,
"poster_id": 4,
"qr_code": "code5",
"worker_id": 0
},
"worker": []
},
{
"poster": {
"job_id_map": 5,
"latitude": 43.68347235901327,
"locationDescription": "Bathurst and St Clair",
"longitude": -79.41851002097324,
"poster_id": 5,
"qr_code": "code6",
"worker_id": 0
},
"worker": []
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.6958481647278,
"locationDescription": "Dufferin and Eglinton",
"longitude": -79.45035794609939,
"poster_id": 6,
"qr_code": "code7",
"worker_id": 0
},
"worker": []
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Calgary",
"job_id": 4,
"owner_id_map": 2,
"status": 0,
"title": "10 Posters in Calgary"
},
"posters": [
{
"poster": {
"job_id_map": 2,
"latitude": 43.64526533333377,
"locationDescription": "Union Station",
"longitude": -79.38060468539248,
"poster_id": 1,
"qr_code": "code2",
"worker_id": 1
},
"worker": [
{
"workerName": "Noris",
"worker_id": 1
}
]
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.64295669900133,
"locationDescription": "Queen and Spadina",
"longitude": -79.39368099382459,
"poster_id": 2,
"qr_code": "code3",
"worker_id": 2
},
"worker": [
{
"workerName": "Jack",
"worker_id": 2
}
]
},
{
"poster": {
"job_id_map": 3,
"latitude": 43.76892443268843,
"locationDescription": "North York Civic Center",
"longitude": -79.41292484238735,
"poster_id": 3,
"qr_code": "code4",
"worker_id": 3
},
"worker": [
{
"workerName": "Nicky",
"worker_id": 3
}
]
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
}
],
"status": "success"
}
有人知道为什么会这样吗?为什么卷曲响应会有所不同?
这是来自 Firebase 的 json
[
{
"job": {
"city": "Toronto",
"job_id": 1,
"owner_id_map": 1,
"status": 0,
"title": "500 Posters in Toronto"
},
"posters": [
{
"poster": {
"job_id_map": 2,
"latitude": 43.64526533333377,
"locationDescription": "Union Station",
"longitude": -79.38060468539248,
"poster_id": 1,
"qr_code": "code2",
"worker_id": 1
},
"worker": [
{
"workerName": "Noris",
"worker_id": 1
}
]
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.64295669900133,
"locationDescription": "Queen and Spadina",
"longitude": -79.39368099382459,
"poster_id": 2,
"qr_code": "code3",
"worker_id": 2
},
"worker": [
{
"workerName": "Jack",
"worker_id": 2
}
]
},
{
"poster": {
"job_id_map": 3,
"latitude": 43.76892443268843,
"locationDescription": "North York Civic Center",
"longitude": -79.41292484238735,
"poster_id": 3,
"qr_code": "code4",
"worker_id": 3
},
"worker": [
{
"workerName": "Nicky",
"worker_id": 3
}
]
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Montreal",
"job_id": 2,
"owner_id_map": 1,
"status": 1,
"title": "20 Posters in Montreal"
},
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Ottawa",
"job_id": 3,
"owner_id_map": 2,
"status": 2,
"title": "350 Posters in Ottawa"
},
"posters": [
{
"poster": {
"job_id_map": 4,
"latitude": 43.63904362027761,
"locationDescription": "Harbourfront",
"longitude": -79.38193214610098,
"poster_id": 4,
"qr_code": "code5",
"worker_id": 0
}
},
{
"poster": {
"job_id_map": 5,
"latitude": 43.68347235901327,
"locationDescription": "Bathurst and St Clair",
"longitude": -79.41851002097324,
"poster_id": 5,
"qr_code": "code6",
"worker_id": 0
}
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.6958481647278,
"locationDescription": "Dufferin and Eglinton",
"longitude": -79.45035794609939,
"poster_id": 6,
"qr_code": "code7",
"worker_id": 0
}
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
},
{
"job": {
"city": "Calgary",
"job_id": 4,
"owner_id_map": 2,
"status": 0,
"title": "10 Posters in Calgary"
},
"posters": [
{
"poster": {
"job_id_map": 2,
"latitude": 43.64526533333377,
"locationDescription": "Union Station",
"longitude": -79.38060468539248,
"poster_id": 1,
"qr_code": "code2",
"worker_id": 1
},
"worker": [
{
"workerName": "Noris",
"worker_id": 1
}
]
},
{
"poster": {
"job_id_map": 2,
"latitude": 43.64295669900133,
"locationDescription": "Queen and Spadina",
"longitude": -79.39368099382459,
"poster_id": 2,
"qr_code": "code3",
"worker_id": 2
},
"worker": [
{
"workerName": "Jack",
"worker_id": 2
}
]
},
{
"poster": {
"job_id_map": 3,
"latitude": 43.76892443268843,
"locationDescription": "North York Civic Center",
"longitude": -79.41292484238735,
"poster_id": 3,
"qr_code": "code4",
"worker_id": 3
},
"worker": [
{
"workerName": "Nicky",
"worker_id": 3
}
]
}
],
"workers": [
{
"workerName": "Noris",
"worker_id": 1
},
{
"workerName": "Jack",
"worker_id": 2
},
{
"workerName": "Nicky",
"worker_id": 3
},
{
"workerName": "Davis",
"worker_id": 4
},
{
"workerName": "Argyle",
"worker_id": 5
},
{
"workerName": "James",
"worker_id": 6
}
]
}
]
这是我的 Retrofit API 调用
@GET("/jobs.json")
suspend fun getJobsList() : Response<List<JobPW>>
我假设嵌套对象存在问题,但我不知道如何调试它。
这是我的模型;这也是房间对象
data class JobPW(
@Embedded val job: Job,
@Relation(
parentColumn = "job_id",
entityColumn = "worker_id",
associateBy = Junction(JobWorkerCrossRef::class)
)
val workers: List<Worker>,
@Relation(
entity = Poster::class,
parentColumn = "job_id",
entityColumn = "job_id_map"
)
val posters: List<PosterWithWorker>
) : Serializable
data class PosterWithWorker(
@Embedded val poster: Poster,
@Relation(
parentColumn = "worker_id",
entityColumn = "worker_id",
associateBy = Junction(
value = PosterWorkerCrossRef::class,
parentColumn = "poster_id_map",
entityColumn = "worker_id_map"
)
)
val worker: List<Worker>
) : Serializa
package com.davidozersky.posterpal.database.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
import java.io.Serializable
@Entity(
foreignKeys = [
ForeignKey(
entity = Owner::class,
parentColumns = ["owner_id"],
childColumns = ["owner_id_map"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
]
)
data class Job (
@PrimaryKey val job_id: Long,
val status: Int,
@ColumnInfo(index = true)
val owner_id_map: Long,
val title: String,
val city: String
) : Serializable
package com.davidozersky.posterpal.database.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.io.Serializable
@Entity
class Worker(
@PrimaryKey
@ColumnInfo(name = "worker_id")
val worker_id: Long,
val workerName: String,
) : Serializable
海报
package com.davidozersky.posterpal.database.entities
import androidx.room.*
@Entity(
foreignKeys = [
ForeignKey(
entity = Job::class,
parentColumns = ["job_id"],
childColumns = ["job_id_map"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
]
)
data class Poster (
@PrimaryKey val poster_id: Long,
@ColumnInfo(index = true)
val job_id_map: Long,
val qr_code: String,
val worker_id: Long,
val latitude: Double,
val longitude: Double,
val locationDescription: String,
)
我尝试将 OkHttp 直接与 HttpLogger 一起使用,调用以 200 响应,并注销相同的 json
[{"job":{"city":"Toronto","job_id":1,"owner_id_map":1,"status":0,"title":"500 Posters in Toronto"},"posters":[{"poster":{"job_id_map":2,"latitude":43.64526533333377,"locationDescription":"Union Station","longitude":-79.38060468539248,"poster_id":1,"qr_code":"code2","worker_id":1},"worker":[{"workerName":"Noris","worker_id":1}]},{"poster":{"job_id_map":2,"latitude":43.64295669900133,"locationDescription":"Queen and Spadina","longitude":-79.39368099382459,"poster_id":2,"qr_code":"code3","worker_id":2},"worker":[{"workerName":"Jack","worker_id":2}]},{"poster":{"job_id_map":3,"latitude":43.76892443268843,"locationDescription":"North York Civic Center","longitude":-79.41292484238735,"poster_id":3,"qr_code":"code4","worker_id":3},"worker":[{"workerName":"Nicky","worker_id":3}]}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Montreal","job_id":2,"owner_id_map":1,"status":1,"title":"20 Posters in Montreal"},"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Ottawa","job_id":3,"owner_id_map":2,"status":2,"title":"350 Posters in Ottawa"},"posters":[{"poster":{"job_id_map":4,"latitude":43.63904362027761,"locationDescription":"Harbourfront","longitude":-79.38193214610098,"poster_id":4,"qr_code":"code5","worker_id":0}},{"poster":{"job_id_map":5,"latitude":43.68347235901327,"locationDescription":"Bathurst and St Clair","longitude":-79.41851002097324,"poster_id":5,"qr_code":"code6","worker_id":0}},{"poster":{"job_id_map":2,"latitude":43.6958481647278,"locationDescription":"Dufferin and Eglinton","longitude":-79.45035794609939,"poster_id":6,"qr_code":"code7","worker_id":0}}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Calgary","job_id":4,"owner_id_map":2,"status":0,"title":"10 Posters in Calgary"},"posters":[{"poster":{"job_id_map":2,"latitude":43.64526533333377,"locationDescription":"Union Station","longitude":-79.38060468539248,"poster_id":1,"qr_code":"code2","worker_id":1},"worker":[{"workerName":"Noris","worker_id":1}]},{"poster":{"job_id_map":2,"latitude":43.64295669900133,"locationDescription":"Queen and Spadina","longitude":-79.39368099382459,"poster_id":2,"qr_code":"code3","worker_id":2},"worker":[{"workerName":"Jack","worker_id":2}]},{"poster":{"job_id_map":3,"latitude":43.76892443268843,"locationDescription":"North York Civic Center","longitude":-79.41292484238735,"poster_id":3,"qr_code":"code4","worker_id":3},"worker":[{"workerName":"Nicky","worker_id":3}]}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]}]
@GET("/jobs.json")
suspend fun getJobsList() : Response<List<JobPW>>
为什么你用
Response
类来包装你的列表?也许你的Response
班级和retrofit.Response
班级不一样。在这种情况下检查您的导入。
其次,检查您添加到 OkHttp 的拦截器,看看您是否没有破坏响应。
如果它们对您不起作用,请告诉您如何从 Retrofit 获取 JsonObject 并显示该代码。