Android, Can't get weather from google api

走远了吗. 提交于 2019-12-09 19:42:17

问题


The following code work well before:

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;
        }

But now It return the String: Unsupported API .

Please help me. Is google api closed? PS. I'm in China.


回答1:


Yes, it seems like Google has stopped the undocumented weather api service.

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

You may want to try yahoo weather or http://www.wunderground.com/weather/api. However, google provided around 4 days weather forecast while yahoo just provides around 2 days forecast (there may be some other way to get more days' forecast from yahoo indirectly).




回答2:


I think the end of the google weather api has come and google has stopped the service. After all, it wasn't even an officially supported api any way.

iGoogle doesn't seem to use the api anymore either. Your best option will probably be to look for a different free weather api.

I'm sure we'll hear more of this change in the next few days.



来源:https://stackoverflow.com/questions/12139862/android-cant-get-weather-from-google-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!