【发布时间】:2021-08-05 05:19:57
【问题描述】:
大家好,我正在尝试从 API 获取数据。我可以使用这个https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json json 文件,但我不能让它工作。 https://www.episodate.com/api/most-popular?page=1
这是我的主要活动
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)
}
我的数据类
data class MovieModel(
val name: String
)
和 API 类
interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<ArrayList<MovieModel>>
当我尝试https://raw.githubusercontent.com/atilsamancioglu/K21-JSONDataSet/master/crypto.json 这个文件时,我可以获得数据,但每当我尝试这个https://www.episodate.com/api/most-popular?page=1 时,应用程序就会崩溃。请看一下谢谢。
【问题讨论】:
-
不能为不同的响应体使用同一个模型类,为第二个API生成另一个模型类。
-
我做到了。我也尝试了来自 json 插件的 kotlin 数据类文件,并且我还尝试了这个数据类工作的另一个 json 文件。但是一些 json 文件会导致崩溃。没找到
标签: java android kotlin retrofit kotlin-coroutines