【问题标题】:Android, Can't get weather from google apiAndroid,无法从谷歌 api 获取天气
【发布时间】:2012-08-21 19:35:04
【问题描述】:

以下代码之前运行良好:

class RetreiveWeatherTask extends AsyncTask<Bundle, Void, WeatherData> {

        private static final String TAG = "WeatherManager";

        @Override
        protected WeatherData doInBackground(Bundle... params) {
            Utils.log(TAG, "start get weather");
            WeatherData weatherData = new WeatherData();
            Bundle b = new Bundle(params[0]);
            double latitude = b.getDouble(REF_LAT);
            double longtitude = b.getDouble(REF_LONG);

            try
            {
                StringBuilder weatherBuilder = new StringBuilder();
                //weatherBuilder.append("http://www.google.com/ig/api?hl=zh-cn&weather=,,,");
                weatherBuilder.append("http://www.google.com/ig/api?hl=en-us&weather=,,,");
                int la = (int) latitude * 1000000;
                int lo = (int) longtitude * 1000000;
                weatherBuilder.append(la);
                weatherBuilder.append(",");
                weatherBuilder.append(lo);
                String weather = weatherBuilder.toString();
                HttpGet hg = new HttpGet(weather);
                // HttpGet hg = new
                // HttpGet("http://www.google.com/ig/api?hl=zh-cn&weather=,,,39130000,117200000");
                HttpClient hc = new DefaultHttpClient();
                HttpResponse hr = hc.execute(hg);
                String responseString = "";
                if (hr.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
                {
                    Utils.logi("", "Location != HttpStatus.SC_OK");
                    return null;
                }
                responseString = EntityUtils.toString(hr.getEntity());
                InputStream is = new ByteArrayInputStream(responseString.getBytes());

                // URL url = new
                // URL("http://www.google.com/ig/api?hl=zh-cn&weather=,,,39130000,117200000");
                // InputStream is =url.openStream();
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document document = db.parse(is);

                NodeList currList = (NodeList) document.getElementsByTagName("current_conditions");
                NodeList currList2 = currList.item(0).getChildNodes();

                weatherData.weather = new String(currList2.item(0).getAttributes().item(0).getNodeValue());

                weatherData.curTemperature = new String(currList2.item(2).getAttributes().item(0).getNodeValue() + "℃");

                weatherData.wind = new String(currList2.item(5).getAttributes().item(0).getNodeValue());

                Utils.log(TAG, "Get weather = " + weatherData.weather);
                if (isNeedIconFromNet)
                {
                    weatherData.iconUrl = new String(currList2.item(4).getAttributes().item(0).getNodeValue());


                    String url = "http://www.google.com" + weatherData.iconUrl;
                    weatherData.icon = getBitmapByUrl(url);
                }

            }
            catch (Exception e)
            {
                Utils.logi("", "Location get temp Exception e");
                e.printStackTrace();
            }

            return weatherData;
        }

但现在它返回 String: Unsupported API 。

请帮助我。谷歌api关闭了吗? PS。我在中国。

【问题讨论】:

    标签: android weather-api


    【解决方案1】:

    我认为谷歌天气 api 已经结束,谷歌已经停止了该服务。毕竟,它甚至都不是官方支持的 api。

    iGoogle 似乎也不再使用该 API。 您最好的选择可能是寻找不同的免费天气 api。

    我相信我们会在接下来的几天内听到更多关于这种变化的消息。

    【讨论】:

      【解决方案2】:

      是的,Google 似乎已经停止了未记录的天气 api 服务。

      XML Parsing Error: syntax error
      Location: http://www.google.com/ig/api?weather=delhi
      Line Number 1, Column 1:Unsupported API
      ^
      

      您可能想试试yahoo weatherhttp://www.wunderground.com/weather/api。 但是,google 提供了 4 天左右的天气预报,而 yahoo 只提供了 2 天左右的天气预报(可能有其他方法可以间接从 yahoo 获得更多天的天气预报)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-30
        • 2012-05-17
        • 2011-07-29
        • 1970-01-01
        • 2011-06-25
        • 2011-12-04
        • 1970-01-01
        相关资源
        最近更新 更多