我正在尝试学习使用Kotlin编写android应用程序的代码。我想显示一些我从api(https://api.kuroganehammer.com/api/characters)获得的数据。我使用IntelliJ中的JSON到kotlin插件生成了数据类。
Logcat显示我正在获取数据,但它不会触发onResponse方法。
我尝试调试,但由于某种原因,onFailure和onResponse方法被忽略了,所以除了找到其他东西以外,我什么都找不到。
ApiRequest:
fun fetchAllCharacters(): Call<CharacterApiRequest>
CharacterApiRequest:
@SerializedName("character")
val characters: List<Character>
)
字符(片段,在onViewCreated()中被调用):
.enqueue(object : Callback<CharacterApiRequest> {
override fun onFailure(call: Call<CharacterApiRequest>, t: Throwable) {
//Display an error to the user, because there was a io exception
}
override fun onResponse(call: Call<CharacterApiRequest>, response: Response<CharacterApiRequest>) {
//We got a response
if (response.isSuccessful) {
//Bind the data only when we have it
response.body()?.apply {
adapter.setData(this.characters as MutableList<Character>)
}
} else {
//Display an error
}
}
})
答案 0 :(得分:0)
如@MateuszHerych的注释所述,将回叫的通用参数从CharacterApiRequest更改为List可以正常工作