我正在使用https://www.api-football.com来获取足球国家和联赛,Json对国家的回应如下:
{
"get": "countries",
"parameters": [],
"errors": [],
"results": 164,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"name": "Albania",
"code": "AL",
"flag": "https://media.api-sports.io/flags/al.svg"
},
{
"name": "Algeria",
"code": "DZ",
"flag": "https://media.api-sports.io/flags/dz.svg"
},
{
"name": "Andorra",
"code": "AD",
"flag": "https://media.api-sports.io/flags/ad.svg"
},
...我试着创建这样一个模型:
下面是CountryData数据类:
data class CountryData (
val code: String,
val flag: String,
val name: String
)下面是Country数据类,它使用CountryData并在列表中使用它们:
data class Country(
val results : List<CountryData>
)但是这些模型给了我一些错误:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was NUMBER at line 1 column 61 path $.results
我认为这是因为api响应类型不是CountryData的列表。
如果这就是问题所在,,我只想在 "response": [ ...之后得到这个角色,但是我不知道怎么做。
如果您认为是另一个问题导致了这个错误,请告诉我。
顺便提一下,这里是api接口:
interface FootballApi {
@Headers("X-RapidAPI-Key: $API_KEY")
@GET(GET_COUNTRIES)
suspend fun getCountries() : Response<Country>
}下面是我的Retrofit实例:
object RetrofitInstance {
val api: FootballApi by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(FootballApi::class.java)
}
}发布于 2022-09-22 15:42:57
我通过向我的项目添加其他数据模型来解决这个问题。下面是Country数据类:
data class Country(
val code: String,
val flag: String,
val name: String
)下面是CountryResponse数据类:
data class CountryResponse (
val get: String,
val parameters: List<Any>,
val errors: List<Any>,
val results: Int,
val paging: Paging,
val response: List<Country>
)下面是Paging数据类:
data class Paging(
val current: Int,
val total: Int
)现在我可以得到response.body()!!.response的国家名单了
https://stackoverflow.com/questions/73816434
复制相似问题