data class NewsEverything (val name : String,
val title : String,
val description : String,
val url : String,
val urlToImage : String,
val publishedAt : String,
val content : String)
interface EverythingAPI {
@GET("***")
suspend fun getEverything() : Response<List<NewsEverything>>
}
private fun loadEverythingData(){
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(EverythingAPI::class.java)
job = CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getEverything()
withContext(Dispatchers.Main){
if(response.isSuccessful){
response.body()?.let {it->
newsEverything = ArrayList(it)
newsEverything?.let {
everythingAdapter = EverythingAdapter(binding.root.context,it)
binding.everythingRV.adapter= everythingAdapter
}
}
}
}
}
}
我知道这个问题是因为我正在使用List,但我不能这样做。有人能帮我写代码吗?
1条答案
按热度按时间e4yzc0pl1#
您得到的错误消息“expected开始_ARRAY but was BEGIN_OBJECT at line 1 column 2”通常表示来自服务器的JSON响应是对象({})而不是数组([])。要解决这个问题,您需要更新代码以正确处理响应。
修复步骤
JsonToKotlinClass
这里