首页 > 解决方案 > 运行调试器时解析 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) 会导致程序按预期运行。仍在寻找答案,但这暂时可行。

标签: androiddebuggingkotlinxml-parsing

解决方案


推荐阅读