嘿,我正在尝试从谷歌解析天气状况。但它一直给我“错误”消息,这是我在 try catch 块上定义的。我在下面打印为异常。
我已经调试了我的应用程序并找出了
xr.parse(new InputSource(website.openStream()));
在那条线上,我的应用程序可以捕获。但不确定是什么问题。
public void onClick(View v) {
String c = city.getText().toString();
String s = state.getText().toString();
StringBuilder URL = new StringBuilder(baseURL);
URL.append(c + "," + s);
String fullUrl = URL.toString();
try {
URL website = new URL(fullUrl);
// getting xml reader to parse data
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlHandler doingWork = new XmlHandler();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
String information = doingWork.getInformation();
tv.setText(information);
} catch (Exception e) {
tv.setText("error");
}
xml处理程序.java
public class XmlHandler extends DefaultHandler {
private XmlData info = new XmlData();
public String getInformation() {
return info.dataToString();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("city")) {
String city = attributes.getValue("data");
info.setCity(city);
} else if (localName.equals("temp_f")) {
String t = attributes.getValue("data");
int temp = Integer.parseInt(t);
info.setTemp(temp);
}
}
}
xmlData.java
public class XmlData {
int temp = 0;
String city = null;
public void setCity(String c) {
city = c;
}
public void setTemp(int t) {
temp = t;
}
public String dataToString() {
return "In " + city + " The Current temp in F is" + temp + " degrees";
}
}
这是 LogCat
02-04 17:46:16.124: W/System.err(647): java.io.FileNotFoundException: http://www.google.com/ig/api?weather=lincoln,nebraska
02-04 17:46:16.124: W/System.err(647): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
02-04 17:46:16.124: W/System.err(647): at java.net.URL.openStream(URL.java:653)
02-04 17:46:16.124: W/System.err(647): at com.android.WeatherXMLParsing.onClick(WeatherXMLParsing.java:61)
02-04 17:46:16.124: W/System.err(647): at android.view.View.performClick(View.java:2408)
02-04 17:46:16.124: W/System.err(647): at android.view.View$PerformClick.run(View.java:8816)
02-04 17:46:16.124: W/System.err(647): at android.os.Handler.handleCallback(Handler.java:587)
02-04 17:46:16.124: W/System.err(647): at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 17:46:16.124: W/System.err(647): at android.os.Looper.loop(Looper.java:123)
02-04 17:46:16.124: W/System.err(647): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 17:46:16.124: W/System.err(647): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 17:46:16.124: W/System.err(647): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 17:46:16.124: W/System.err(647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 17:46:16.124: W/System.err(647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 17:46:16.134: W/System.err(647): at dalvik.system.NativeStart.main(Native Method)