ArrayIndexOutOfBoundsException in android's KXmlParser

独自空忆成欢 提交于 2019-12-30 17:59:12

问题


I am doing regular XML parsing on android, and very rarely I get this exception and only on particular phones. Haven't been able to get to the root of this issue. Does anybody have an idea what might be causing this?

    java.lang.ArrayIndexOutOfBoundsException: src.length=8192 srcPos=1 dst.length=8192 dstPos=0 length=-1
    at java.lang.System.arraycopy(Native Method)
    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1489)
    at org.kxml2.io.KXmlParser.skip(KXmlParser.java:1574)
    at org.kxml2.io.KXmlParser.parseStartTag(KXmlParser.java:1049)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:369)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)

回答1:


I have a similar exception. In my case, the xml had been downloaded and saved to the SD. When I opened the file, I saw that it cut out "mid-sentence", as it were. It looked like there had been an error in downloading the file. Deleting the file and re-downloading fixed the issue in this instance, but to prevent future issues of the same type, I check for ArrayIndexOutOfBoundsException when reading an xml, and assume the file is corrupt when it catches one.

It's obviously just a bandaid solution to a vaguely defined problem at the moment. If I figure more out, I'll update my answer.




回答2:


I found a bug-fix for Android 4.4 mentioning this: Change 61530

So the proper exception you should be getting is: Unterminated element content spec, meaning unexpected EOF.




回答3:


The lower layer (kxml) is incorrectly throwing an unchecked Exception when it encounters bad xml. To workaround this, when using XmlPullParser, replace

try {
    // use XmlPullParser
}
catch (XmlPullParserException ex) { ... }

with

try {
    // use XmlPullParser
}
catch (Exception ex) { ... }


来源:https://stackoverflow.com/questions/11967062/arrayindexoutofboundsexception-in-androids-kxmlparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!