I'm using Moshi with Retrofit and trying to query some game data. When I do this I get an error stating the following:
com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.errors
This seems to point to the errors
property within the Games
data class I've created. See here:
data class Games(
val get: String,
val errors: List<Any?>,
val results: Long,
val response: List<GameResponse>,
)
data class GameResponse(
val game: Game,
val league: League,
val teams: Teams,
val scores: Scores,
)
data class Game(
val id: Long,
val stage: String,
val week: String,
val date: Date,
val venue: Venue,
val status: Status,
)
data class Date(
val timezone: String,
val date: String,
val time: String,
val timestamp: Long,
)
data class Venue(
val name: String?,
val city: Any?,
)
data class Status(
val short: String,
val long: String,
val timer: Any?,
)
data class League(
val id: Long,
val name: String,
val season: String,
val logo: String,
val country: Country,
)
data class Country(
val name: String,
val code: String,
val flag: String,
)
data class Teams(
val home: Home,
val away: Away,
)
data class Home(
val id: Long,
val name: String,
val logo: String,
)
data class Away(
val id: Long,
val name: String,
val logo: String,
)
data class Scores(
val home: Home2,
val away: Away2,
)
data class Home2(
val quarter1: Long?,
val quarter2: Long?,
val quarter3: Long?,
val quarter4: Long?,
val overtime: Long?,
val total: Long?,
)
data class Away2(
val quarter1: Long?,
val quarter2: Long?,
val quarter3: Long?,
val quarter4: Long?,
val overtime: Long?,
val total: Long?,
)
When I try making the API call with Postman I get the following response. As a note, I ommited the "parameters" object in my Data Class because I'm passing it as part of the Headers object.
{
"get": "games",
"parameters": {
"date": "2023-11-21"
},
"errors": [],
"results": 1,
"response": [
{
"game": {
"id": 7693,
"stage": "Regular Season",
"week": "Week 11",
"date": {
"timezone": "UTC",
"date": "2023-11-21",
"time": "01:15",
"timestamp": 1700529300
},
"venue": {
"name": "GEHA Field at Arrowhead Stadium",
"city": "Kansas City"
},
"status": {
"short": "FT",
"long": "Finished",
"timer": null
}
},
"league": {
"id": 1,
"name": "NFL",
"season": "2023",
"logo": "https://media-4.api-sports.io/american-football/leagues/1.png",
"country": {
"name": "USA",
"code": "US",
"flag": "https://media-4.api-sports.io/flags/us.svg"
}
},
"teams": {
"home": {
"id": 17,
"name": "Kansas City Chiefs",
"logo": "https://media-4.api-sports.io/american-football/teams/17.png"
},
"away": {
"id": 12,
"name": "Philadelphia Eagles",
"logo": "https://media-4.api-sports.io/american-football/teams/12.png"
}
},
"scores": {
"home": {
"quarter_1": 7,
"quarter_2": 10,
"quarter_3": 0,
"quarter_4": 0,
"overtime": null,
"total": 17
},
"away": {
"quarter_1": 7,
"quarter_2": 0,
"quarter_3": 7,
"quarter_4": 7,
"overtime": null,
"total": 21
}
}
}
]
}
And this is how I've structured the Retrofit Service:
interface ApiSportsService {
@GET("random_joke")
suspend fun getGames(
@HeaderMap headers: Map<String, String>
): Games
@GET("status")
suspend fun getStatus(
@HeaderMap headers: Map<String, String>
): ApiStatus
}
Could someone point me in the right direction as to what I might be doing wrong? Thank you so much in advance!