Android 3.1 Xml 解析 NullPointerException
我为 Android 2.2 编写了代码,该代码应该将 xml 从网页解析为字符串。它在 Android 2.2 模拟器上运行良好,但在 Android 3.1 平板电脑上却出现 NullPointerException。代码如下:
Log.d("refreshMeta", "refreshing meta.");
url = new URL("http://www.chineseoutreach.ca/media/Cstreaming.xml");
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
Log.d("Connection","connected");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("nowplaying");
Element entry = (Element)nl.item(0);
try {
Element eartist = (Element)entry.getElementsByTagName("artist").item(0);
sartist = eartist.getFirstChild().getNodeValue();
Log.d("Artist",sartist);
}
catch(NullPointerException e) {
sartist = "";
Log.d("Connection",e.toString());
}
登录Android 3:
refreshmeta:刷新meta。
连接:已连接
连接:java.lang.NullPointerException
编辑 这是导致它的线路。 元素eartist = (Element)entry.getElementsByTagName("艺术家").item(0);
编辑2
07-28 13:10:01.483:错误/空(6189):我的消息
07-28 13:10:01.483:错误/空(6189):java.lang.NullPointerException
07-28 13:10:01.483:错误/空(6189):位于com.ciam.app.CiamInfoActivity.refreshMeta(CiamInfoActivity.java:280)
07-28 13:10:01.483:错误/空(6189):在com.ciam.app.CiamInfoActivity.access $ 0(CiamInfoActivity.java:257)
07-28 13:10:01.483:错误/空(6189):位于 com.ciam.app.CiamInfoActivity$2.run(CiamInfoActivity.java:123) 07-28 13:10:01.483: ERROR/Null(6189): at java.lang.Thread.run(Thread.java:1020)
我怀疑在 android 2 和 android 3 上解析 xml 的方式之间存在一些差异有什么想法吗?提前致谢。
I have code written for Android 2.2 that is supposed to parse xml from a webpage to a String. It works fine on an Android 2.2 emulator, but it gives me a NullPointerException on my Android 3.1 tablet. Here is the code:
Log.d("refreshMeta", "refreshing meta.");
url = new URL("http://www.chineseoutreach.ca/media/Cstreaming.xml");
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
Log.d("Connection","connected");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("nowplaying");
Element entry = (Element)nl.item(0);
try {
Element eartist = (Element)entry.getElementsByTagName("artist").item(0);
sartist = eartist.getFirstChild().getNodeValue();
Log.d("Artist",sartist);
}
catch(NullPointerException e) {
sartist = "";
Log.d("Connection",e.toString());
}
Log on Android 3:
refreshmeta: refreshing meta.
Connection: Connected
Connection: java.lang.NullPointerException
EDIT
This is the line that causes it.
Element eartist = (Element)entry.getElementsByTagName("artist").item(0);
Edit 2
07-28 13:10:01.483: ERROR/Null(6189): my message
07-28 13:10:01.483: ERROR/Null(6189): java.lang.NullPointerException
07-28 13:10:01.483: ERROR/Null(6189): at com.ciam.app.CiamInfoActivity.refreshMeta(CiamInfoActivity.java:280)
07-28 13:10:01.483: ERROR/Null(6189): at com.ciam.app.CiamInfoActivity.access$0(CiamInfoActivity.java:257)
07-28 13:10:01.483: ERROR/Null(6189): at com.ciam.app.CiamInfoActivity$2.run(CiamInfoActivity.java:123)
07-28 13:10:01.483: ERROR/Null(6189): at java.lang.Thread.run(Thread.java:1020)
I suspect that there are some differences between the way xml is parsed on android 2 and android 3. Any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中有一个小错误。我真的不明白为什么它可以与 android 2.1 emu 一起使用,但它不应该这样。您的问题是您正在执行 dom.getDocumentElement()。该函数将返回 xml 文件的基本标记,在本例中为“nowplaying”。然后你正在做一个“docEle.getElementsByTagName(“nowplaying”);”在那之上。
如果您想确保获得“nowplaying”,则必须执行“dom.getElementsByTagName(“nowplaying”)”。
更正版本:
There is a small mistake in your code. I don't really understand why it was working with android 2.1 emu, but it shouldn't have. Your problem was that you were doing a dom.getDocumentElement(). That function will return the base tag of your xml file which is in this case "nowplaying". And then you are doing a "docEle.getElementsByTagName("nowplaying");" over that.
If you want to be sure to get "nowplaying", you have to do a "dom.getElementsByTagName("nowplaying")".
Corrected version:
我不完全确定,但它可能必须对链接执行某些操作,因为它可以建立连接,但找不到文件...尝试记录是否可以找到 url 指向的 xml。
如果找不到该文件,请尝试删除 .xml 扩展名,也许可以。
I'm not entirely sure, but it might have to do something with the link, since it can make the connection, but can't find the file... Try logging if it can find the xml the url points to.
If it can't find the file, try removing the .xml extension, perhaps it works.
我遇到了类似的问题并偶然发现了此页面。
根据我的观察,不同 Android 版本之间 getElementsByTagName 的行为有所不同。
在版本 2 中,如果结果具有指定的标签,则结果包含自身。在你的情况下,我猜变量“docEle”有标签“nowplaying”,所以它工作正常。
在版本 3 中,结果仅从后代中搜索,因此不包含自身 (docEle)。所以“nl”实际上是空的。
根据规范,版本 3 似乎是预期的行为。
这似乎很重要,但没有其他人提到这一点。或者,我可能错了......
I'm having the similar issue and stumbled upon this page.
In my observation, the behaviour of getElementsByTagName is different among different android versions.
In ver 2, the result incudes itself if it has the specified tag. In your case, I guess the variable "docEle" has the tag "nowplaying" so it works fine.
In ver 3, the result only searches from the descendants so it doesn't include itself (docEle). So "nl" is really emptly.
According to the specification, ver 3 seems to be the expected behaviour.
This seems so crucial but no one else mentioned this. Or, I may be wrong...