【发布时间】:2020-01-19 09:47:04
【问题描述】:
我跟着教程:https://android.jlelse.eu/android-networking-in-2019-retrofit-with-kotlins-coroutines-aefe82c4d777
我需要以JSON 格式恢复数据,我收到以下错误消息:
com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $*
我查看了这个答案,但我不知道如何使其适应我的代码:Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY
这是我的界面
interface LocationService {
@GET("locations?countryid=fr")
fun getLocationList() : Deferred<Response<LocationResponse>>
}
位置响应
data class LocationResponse (val results : List<Location>)
位置模型
data class Location (
@field:Json(name = "id") val id : String,
@field:Json(name = "category") val category : String,
@field:Json(name = "label") val label : String,
@field:Json(name = "value") val value : String
)
JSON 是这样的
[
{
"id":"city_39522",
"category":"Villes",
"label":"Parisot (81310)",
"value":null
},
{
"id":"city_36661",
"category":"Villes",
"label":"Paris 9ème (75009)",
"value":null
},
{
"id":"city_39743",
"category":"Villes",
"label":"Parisot (82160)",
"value":null
}
]
我已经得到一个列表,我不知道如何纠正错误?
【问题讨论】:
标签: java android kotlin mvvm retrofit2