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