Android “有麻烦”;For循环范围必须有一个';迭代器()';方法“;和JSON问题
我在自学Kotlin和android dev。所以,我确信我的大部分问题都是缺乏知识,但这一部分我已经挂断了一两天了。我认为我的问题部分是我的JSON查询,大部分是我的新手 在下面的for循环中,我从IDE中得到以下错误“for循环范围必须有一个‘iterator()’方法”。这与:for中的“cycloneList”有关(cycloneList中的stormInfo) 我已经链接了我正在使用的“虚拟”JSON数据,可以在这里找到:为了在问题中节省一点空间 问题代码Android “有麻烦”;For循环范围必须有一个';迭代器()';方法“;和JSON问题,android,json,kotlin,retrofit,Android,Json,Kotlin,Retrofit,我在自学Kotlin和android dev。所以,我确信我的大部分问题都是缺乏知识,但这一部分我已经挂断了一两天了。我认为我的问题部分是我的JSON查询,大部分是我的新手 在下面的for循环中,我从IDE中得到以下错误“for循环范围必须有一个‘iterator()’方法”。这与:for中的“cycloneList”有关(cycloneList中的stormInfo) 我已经链接了我正在使用的“虚拟”JSON数据,可以在这里找到:为了在问题中节省一点空间 问题代码 `var cycloneLi
`var cycloneList = response?.body()?.currenthurricane?.stormInfo?.get(0)
if (cycloneList != null) {
for (stormInfo in cycloneList) { <<--Problem
val newCyclone = "Name: ${cycloneList.stormName}"
cycloneStrings.add(newCyclone)
}
}`
interface WeatherWunderGroundAPI {
@GET("bins/19uurt")
fun getCyclones() : Call<Cyclones>
}
class Cyclones(val currenthurricane: CurrentHurricane)
class CurrentHurricane(val stormInfo: List<StormInfo>)
class StormInfo(val stormName: String)
class CycloneRetriever {
val service : WeatherWunderGroundAPI
init {
val = retrofitCyclone
Retrofit.Builder().baseUrl("https://api.myjson.com/")
.addConverterFactory(GsonConverterFactory.create()).build()
service = retrofitCyclone.create(WeatherWunderGroundAPI::class.java)
}
fun getCyclones(callback: Callback<Cyclones>) {
val call = service.getCyclones()
call.enqueue(callback)
}
}
`var cycloneList=response?.body()?.currenthurricane?.stormInfo?.get(0)
if(cycloneList!=null){
对于(cycloneList中的stormInfo){您的stormInfo
是一个列表
,它提供了必需的迭代器()
,这就是您要迭代的内容:
var cycloneList = response?.body()?.currenthurricane?.stormInfo ?: emptyList()
for (stormInfo in cycloneList) { }
希望它能帮助…非常清楚的回答。谢谢。我仍然需要弄清楚我的JSON查询,但是你的回答澄清了很多。我完全误解了IDE的要求。
var cycloneList = response?.body()?.currenthurricane?.stormInfo ?: emptyList()
for (stormInfo in cycloneList) { }