0

嘿,我正在尝试从谷歌解析天气状况。但它一直给我“错误”消息,这是我在 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)
4

2 回答 2

0

当我尝试打开该 URL 时,我收到此消息

我们很抱歉...

...但您的计算机或网络可能正在发送自动查询。为了保护我们的用户,我们现在无法处理您的请求。

而不是获得 XML 响应。确保您获得适当的 API 密钥(如果有),以便服务器不会返回此响应。

于 2013-02-04T15:55:34.813 回答
0

很遗憾地报告该 API 已在不久前被删除。

更多信息在这里。它在当时引起了一些不安,但毕竟它是一个(非常好的)私有API……从来没有任何文档或支持它。

于 2013-02-04T16:35:00.047 回答