【发布时间】:2014-07-15 17:39:50
【问题描述】:
我查看了此处发布的几个答案,但找不到我需要的答案。它可能与网站本身有关,但我认为不是。 我正在尝试解析网站上的 XML,但遇到 空指针异常 错误。
我运行解析是一个单独的线程,在从网络上阅读时遵循 Google 的需求。
请查看我的代码并尝试提供帮助。
class BackgroundTask1 extends AsyncTask<String, Void, String[]> {
protected String[] doInBackground(String... url) {
new HttpGet();
new StringBuffer();
InputStream is = null;
HttpURLConnection con = null;
try {
//Log.d("eyal", "URL: " + boiUrl);
URL url1 = new URL("http://www.boi.org.il/currency.xml");
con = (HttpURLConnection)url1.openConnection();
con.setRequestMethod("GET");
con.connect();
is = con.getInputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
NodeList lastVld = doc.getElementsByTagName("LAST_UPDATE");
String lastV = lastVld.item(0).getFirstChild().getNodeValue();
}
catch (Exception e) {
e.printStackTrace();
}
我在最后一行得到错误。
感谢您的帮助。
【问题讨论】:
-
您在代码中的哪个位置获得了 NPE?
-
Ryan J,回答你的问题- 在 lastVld.item(0).getFirstChild().getNodeValue();
标签: java android xml parsing xml-parsing