android - 运行调试器时解析 XML 并获得不同的结果,与仅运行
问题描述
作为一个更大项目的一部分,我使用 XMLPullParser 从 BBC 新闻 rss 提要中读取数据。当我点击运行时,我会得到最后一项的重复列表(标题、描述、pubDate 和链接)。但是,当我使用 Log.i() 在行上设置断点时,我的代码可以完美运行,并使用正确的变量提取每篇文章。知道为什么它在调试与运行时会以不同的方式运行吗?
publishProgress(100) 是触发将文章添加到实际应用程序中的 ListView 的原因。这是我正在解析的 rss 提要的链接:http: //feeds.bbci.co.uk/news/world/us_and_canada/rss.xml
try {
val url = URL(bbcUrl)
val urlConnection = url.openConnection() as HttpURLConnection
val inStream = urlConnection.inputStream
//Set up the XML parser:
val factory = XmlPullParserFactory.newInstance()
factory.isNamespaceAware = false
val xpp = factory.newPullParser()
xpp.setInput(inStream, "UTF-8")
var eventType: Int = xpp.eventType //While not the end of the document:
while (eventType != XmlPullParser.END_DOCUMENT) {
eventType = xpp.eventType
when (eventType) {
XmlPullParser.START_TAG //This is a start tag < ... >
-> {
if(xpp.name == "item")
Log.i("newItem", "New Article Found") //Breakpoint goes here
when (xpp.name) {
"title" -> {
xpp.next()
title = xpp.text
publishProgress(25)
}
"description" -> {
xpp.next()
description = xpp.text
publishProgress(50)
}
"link" -> {
xpp.next()
link = xpp.text
publishProgress(75)
}
"pubDate" -> {
xpp.next()
pubDate = xpp.text
publishProgress(100)
}
null -> {
}
}
}
XmlPullParser.END_TAG //This is an end tag: </ ... >
-> {
}
XmlPullParser.TEXT //This is text between tags < ... > Hello world </ ... >
-> {
}
}
xpp.next() // move the pointer to next XML element
}
}
catch(ex: MalformedURLException){
ret = "MalformedUrlException"
}
catch(ex : IOException ){
ret = "IO Exception"
}
catch(ex : XmlPullParserException){
ret = "XML Pull Exception. The XML is not properly formed"
}
编辑:从来没有解决这个问题的根源,但是在我的 Log.i() 旁边的 while 循环中添加 sleep(1) 会导致程序按预期运行。仍在寻找答案,但这暂时可行。
解决方案
推荐阅读
- ios - 在开始一个新的 react-native 项目时,我是否必须按照 Apple 将我的部署目标更改为 iOS 14?
- flutter - 在 Flutter 中向上推 Column 的子项
- android - 我的 android studio 项目(kotlin)中缺少功能
- javascript - 在由 transitionEnd 事件触发时,requestAnimationCallback 后更改元素显示和设置转换不起作用
- python - 标签 Tkinter 问题
- java - Java 字节码错误:期望在堆栈上找到对象/数组
- bash - 如何循环包含名称列表的 file.txt?
- image - 如何避免 UNet 算法偏向于背景,而不是罕见的异常?
- python - Dockerfile 和诗歌 | 执行器运行失败 [/bin/sh -c 诗歌安装 --no-dev]:退出代码:1
- android - Android 内容无法正确显示