微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JSONArray 上的线程中的 For 循环仅迭代一次,我不知道为什么

如何解决JSONArray 上的线程中的 For 循环仅迭代一次,我不知道为什么

我在 Runnable 类中有 for 循环,它从其他 runnable 的侦听器获取数据,但另一个 runnable 与第一个 runnable 位于同一线程上(它只是调用 .run() 方法来重用一些代码,一切正常直到现在......我得到了包含 2 个重要变量的 JSONObject,其中一个是我保存为“tempResult”的 JSONArray,它有 14 个对象长,并且所述对象永远不会为空。然后当我尝试在我尝试将提取的对象“Vod”添加到 ArrayList 后,遍历它它停止。这是有趣部分的代码...

if (result!!.get("data") is JSONArray) {
                    val vods = ArrayList<Vod>()
                    val tempResult = result.getJSONArray("data")
                    Log.i("Vodviewmodel","onTaskCompleted: ${tempResult.length()}") // always 14
                    for (jsonObject in tempResult) {
                        Log.i("Vodviewmodel","onTaskCompleted: $jsonObject") // can't show it to you but it looks as expected
                        vods.add(Vod.fromJsonObject(jsonObject)) // this method just parses JSONObject and I confirmed that its working properly
                        //loop stops
                        Log.i("Vodviewmodel","onTaskCompleted: ${vods[0]}") // I tried to look if it was added but it never showed in logcat and there was never any error from this method
                    }
                    Log.i("Vodviewmodel","onTaskCompleted: after for") // never was reached
                    listener.done(vods,result.getInt("total_items"))
                }

这里是 JSONArray 的迭代器...

operator fun JSONArray.iterator(): Iterator<JSONObject>
            = (0 until length()).asSequence().map { get(it) as JSONObject }.iterator()

我正在使用 Executor 在单独的线程上启动这个可运行程序,但我真的不知道发生了什么,我对线程没有经验,所以任何事情都可能有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。