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 からのいくつかのエラー