我在 android 中使用 SAX 解析器从 url 获取 xml 数据,但我仍然没有得到响应的存储位置。
代码:
String c=city.getText().toString();
String s=state.getText().toString();
StringBuilder URL=new StringBuilder(baseURL);
URL.append(c+","+s);
try{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
HandlingXMLStuff handler=new HandlingXMLStuff();
reader.setContentHandler(handler);
URL url = new
URL("http://api.openweathermap.org/data/2.5/weathermode=xml&q="+c+","+s);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
reader.parse(new InputSource(connection.getInputStream()));
String information=handler.getInformation();
output.setText(information);
}
catch(Exception e)
{
output.setText(e.toString());
}
我的问题是响应存储在哪里以及如何访问它以从 xml 获取所需的信息?
编辑:非常感谢大家的帮助..现在我理解了逻辑..但我面临另一个问题....
代码:
String c = city.getText().toString();
String s = state.getText().toString();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
HandlingXMLStuff handler = new HandlingXMLStuff();
reader.setContentHandler(handler);
String query = URLEncoder.encode(
"http://api.openweathermap.org/data/2.5/weather?mode=xml&q="
+ c + "," + s, "utf-8");
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?mode=xml&q="
+ c + "," + s);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
reader.parse(new InputSource(connection.getInputStream()));
String information = handler.getInformation();
output.setText(information);
} catch (Exception e) {
output.setText(e.toString());
}
我没有收到来自 url 的响应,出现错误“无法将 null 解析为 int”。知道发生了什么吗?我在浏览器中输入了相同的 url 并得到了正确的响应,但在这里不起作用。
编辑:固定。我的处理程序函数 xD 出现一些错误