private static String URL_START="http://weather.yahooapis.com/forecastrss?p="; private static String URL_END="&u=c"; public static String strInfo; public static void getWeatherByID(String country,String city,String str_id) { InputStream inputStream = null; try { String url_str=URL_START+str_id+URL_END; URL url = new URL(url_str); inputStream = url.openConnection().getInputStream(); if(inputStream == null ) { return ; } XmlPullParser parser = Xml.newPullParser(); parser.setInput(inputStream, "UTF-8"); int event = parser.getEventType(); while(event != XmlPullParser.END_DOCUMENT){ String name = parser.getName(); switch (event) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if(name.equals("title")) { } else if(name.equals("condition")) { strInfo=""; strInfo+=city; strInfo+=";"; strInfo+=parser.getAttributeValue(1); strInfo+=";"; strInfo+=parser.getAttributeValue(2); strInfo+=";"; strInfo+=parser.getAttributeValue(3); strInfo+=";";
}else if(name.equals("forecast")) { } break; case XmlPullParser.END_TAG: break; } event = parser.next(); } }catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); }finally{ try { if(inputStream!=null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
說明:首先得列出一份須要提供地區的id,https://weather.yahoo.com/uae/如這樣 先找到地區簡寫,而後進去看其所提供的國家或地區 ,點擊進入到天氣界面,頁面上有 extendedforecast點擊進入就有 如:http://www.weather.com/weather/extended/AEXX0004? AExx0004就是所在地id AE是國家簡碼 0004是城市編號 默認數字應該就是四位,不足時前面補零。如連接:http://weather.yahooapis.com/forecastrss?p=AEXX0004&u=c就能獲得迪拜天氣的xml,根據需求拿本身想要的數據。其它的城市得去找到id。
java