我试图解析这个API,但我不认为我的数据类设置正确
互联睡眠者API
package com.example.drafttime.api
import com.example.drafttime.data.PlayerData
import com.example.drafttime.data.PlayerInfo
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
// This file is our interface with the Sleeper api to get player data
interface ConnectedSleeper {
@GET("nflTeamDataArray")
fun getPlayerData(
):Call<List<PlayerData>>
companion object{
private const val BASE_URL = "http://10.0.2.2:8080/"
fun create(): ConnectedSleeper {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(ConnectedSleeper::class.java)
}
}
}
kt数据类
package com.example.drafttime.data
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class PlayerData(
val team: List<Player>
):java.io.Serializable
@JsonClass(generateAdapter = true)
data class Player(
val player: List<PlayerInfo>
):java.io.Serializable
PlayerInfo.kt
package com.example.drafttime.data
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class PlayerInfo(
@Json(name = "birth_date") val birthDate: String?,
@Json(name = "full_name") val fullName: String?,
@Json(name = "college") val college: String?,
@Json(name = "team") val team : String?,
@Json(name = "position") val postion: String?,
@Json(name = "number") val number: Int?,
@Json(name = "age") val age: String?,
@Json(name = "weight") val weight: String?,
@Json(name = "height") val height: String?
):java.io.Serializable
主要活动
sleeperConnect.getPlayerData()
.enqueue(object : Callback<List<PlayerData>> {
override fun onResponse(
call: Call<List<PlayerData>>,
response: Response<List<PlayerData>>
) {
///Need to hook up adapter to this part of the response
if (response.isSuccessful) {
Log.d("Response", "onResponse: ${response.body()}")
//TODO: Adapter and recycler view should go here
// val adapter = response.body()?.let { it1 -> CustomAdapter(it1) }
// recyclerview.adapter = adapter
}
}
override fun onFailure(call: Call<List<PlayerData>>, t: Throwable) {
Log.d("Draft time Activity ", "onFailure: " + t.message)
}
})
JSON文件
[
[
{
"rotoworld_id": 4502,
"practice_description": null,
"birth_state": null,
"metadata": null,
"espn_id": 11122,
"birth_country": null,
"team": "ARI",
"search_last_name": "prater",
"player_id": "17",
"gsis_id": "00-0023853",
"age": 38,
"sportradar_id": "67f5e782-f91c-4536-9818-cf4a0e7e821d",
"last_name": "Prater",
"status": "Active",
"fantasy_data_id": 549,
"yahoo_id": 8565,
"pandascore_id": null,
"search_rank": 141,
"position": "K",
"birth_city": null,
"depth_chart_position": "PK",
"practice_participation": null,
"sport": "nfl",
"search_full_name": "mattprater",
"injury_start_date": null,
"rotowire_id": 5051,
"hashtag": "#mattprater-NFL-ARI-5",
"swish_id": 218237,
"high_school": "Estero (FL)",
"number": 5,
"depth_chart_order": 1,
"full_name": "Matt Prater",
"injury_body_part": null,
"search_first_name": "matt",
"years_exp": 17,
"height": "70",
"first_name": "Matt",
"injury_status": null,
"active": true,
"news_updated": 1678853455354,
"weight": "201",
"birth_date": "1984-08-10",
"fantasy_positions": [
"K"
],
"stats_id": 218237,
"college": "UCF",
"injury_notes": null
}
]
]
JSON文件似乎是数组中的对象数组。我以为我已经正确设置了数据类,但我得到了预期的开始_OBJECT,但BEGIN_ARRAY在路径$[0]。我在设置数据类时出错了吗?
我尝试设置API的响应调用List,然后调用List,因为它是一个数组中的对象数组,但它不起作用
1条答案
按热度按时间woobm2wo1#
它嵌套了2个数组,并在1个对象中。尝试使用嵌套数组获得响应:
并更改代码的其他部分以适应这种结构。这应该会直接给予你对象。