改造预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ KOTLIN BEGIN_OBJECT
RETROFIT Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ KOTLIN
大家好,我正在尝试从 API 获取数据。我可以用这个 https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json json file it's work but I can't make this work. https://www.episodate.com/api/most-popular?page=1
这是我的主要内容ACTIVITY
private fun loadData(){
val retrofit = Retrofit.Builder()
.baseUrl("https://www.episodate.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MovieAPI::class.java)
CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getData()
if (response.isSuccessful){
response.body()?.let {
Moviemodel = ArrayList(it)
}
}
println(Moviemodel)
}
我的数据CLASS
data class MovieModel(
val name: String
)
AND API CLASS
interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<ArrayList<MovieModel>>
当我尝试 https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json this file I can get datas but whenever I try this https://www.episodate.com/api/most-popular?page=1 应用程序崩溃。请看一下谢谢
这是一个类似的问题。你的模型应该像
data class Movie(
val page: Int,
val pages: Int,
val total: String,
val tv_shows: List<TvShow>
)
data class TvShow(
val country: String,
val end_date: Any,
val id: Int,
val image_thumbnail_path: String,
val name: String,
val network: String,
val permalink: String,
val start_date: String,
val status: String
)
然后APIclass
interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<Movie>//instead of Response<ArrayList<Movie>>
在activity
CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getData()
if (response.isSuccessful){
response.body()?.let {
shows = List(it.tv_shows)
}
}
for(show in shows){
println(show.name) // here are the names
}
}
如果你想为你的模型使用不同的 属性 名称,你应该用 @SerializedName()
注释这些 属性 名称。更多信息请参考
第一个是 json 列表,第二个是包含列表的 json 对象,因此您不能直接解析为列表。您应该有包含列表对象的 class。
例如:
data class MoviePageModel(
val total: Int,
val page: Int,
val tvShows: List<MovieModel>
)
https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json
如果来自此 URL 的响应,如果您看到响应被方括号括起来,即“[]”。这意味着响应将在 JSON 数组中。
https://www.episodate.com/api/most-popular?page=1
在来自此 URL 的响应的情况下,如果您看到响应用大括号括起来,即“{}”。这意味着响应将在 JSON 对象中。
在下一行中,您提到响应将是 MovieModel 的 ArrayList,但实际上在第一个 URL 的情况下它将是一个 JSON 数组,在第一个 URL 的情况下第二个是 JSON 对象。
暂停有趣的 getData(): Response
为了解决这个问题,您需要为每个URL定义单独的响应模型。
大家好,我正在尝试从 API 获取数据。我可以用这个 https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json json file it's work but I can't make this work. https://www.episodate.com/api/most-popular?page=1
这是我的主要内容ACTIVITY
private fun loadData(){
val retrofit = Retrofit.Builder()
.baseUrl("https://www.episodate.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MovieAPI::class.java)
CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getData()
if (response.isSuccessful){
response.body()?.let {
Moviemodel = ArrayList(it)
}
}
println(Moviemodel)
}
我的数据CLASS
data class MovieModel(
val name: String
)
AND API CLASS
interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<ArrayList<MovieModel>>
当我尝试 https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json this file I can get datas but whenever I try this https://www.episodate.com/api/most-popular?page=1 应用程序崩溃。请看一下谢谢
data class Movie(
val page: Int,
val pages: Int,
val total: String,
val tv_shows: List<TvShow>
)
data class TvShow(
val country: String,
val end_date: Any,
val id: Int,
val image_thumbnail_path: String,
val name: String,
val network: String,
val permalink: String,
val start_date: String,
val status: String
)
然后APIclass
interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<Movie>//instead of Response<ArrayList<Movie>>
在activity
CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getData()
if (response.isSuccessful){
response.body()?.let {
shows = List(it.tv_shows)
}
}
for(show in shows){
println(show.name) // here are the names
}
}
如果你想为你的模型使用不同的 属性 名称,你应该用 @SerializedName()
注释这些 属性 名称。更多信息请参考
第一个是 json 列表,第二个是包含列表的 json 对象,因此您不能直接解析为列表。您应该有包含列表对象的 class。 例如:
data class MoviePageModel(
val total: Int,
val page: Int,
val tvShows: List<MovieModel>
)
https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json
如果来自此 URL 的响应,如果您看到响应被方括号括起来,即“[]”。这意味着响应将在 JSON 数组中。https://www.episodate.com/api/most-popular?page=1
在来自此 URL 的响应的情况下,如果您看到响应用大括号括起来,即“{}”。这意味着响应将在 JSON 对象中。
在下一行中,您提到响应将是 MovieModel 的 ArrayList,但实际上在第一个 URL 的情况下它将是一个 JSON 数组,在第一个 URL 的情况下第二个是 JSON 对象。
暂停有趣的 getData(): Response
为了解决这个问题,您需要为每个URL定义单独的响应模型。