91久久久久久久久_欧美日韩中文_在线观看中文字幕亚洲_夜夜骑首页_精品久久伊人_天天色天天射天天操

當前位置: 首頁IT技術 → Yahoo Service實現天氣預報的實例代碼分享

Yahoo Service實現天氣預報的實例代碼分享

更多

下面提供給分享學習的關于Yahoo Service實現天氣預報的實例代碼,希望能夠給大家帶來幫助或啟發。

天氣預報是非常有用的服務,如果能在網站上集成天氣預報,能極大地方便用戶查詢。

  尋遍了國內所有的氣象站點,沒找見提供Web服務的,太小氣了,只能去國外找。NOAA(www.weather.gov)提供一個Web服務,但是死活連不上服務器,估計被屏蔽了,其他提供全球天氣預報的有www.weather.com和yahoo,

  不過weather.com的服務太麻煩,還需要注冊,相比之下,yahoo的天氣服務既簡單又快速,只需一個http請求,然后解析返回的XML即可獲得天氣預報。

  以北京為例,在weather.yahoo.com查找北京的城市代碼為CHXX0008,對應的URL為:

  http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX0008

  然后,通過SAX解析返回的XML:

  URL url = new URL("http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX0008");
  InputStream input = url.openStream();
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setNamespaceAware(false);
  parser = factory.newSAXParser();
  parser.parse(input, new YahooHandler());

  自己定義一個YahooHandler來響應SAX事件:

  /**
  * For more information, please visit: http://www.crackj2ee.com
  * Author: Liao Xuefeng
   */
  public class YahooHandler extends DefaultHandler {

public void startElement(String uri, String localName, String qName, Attributes attributes)

  throws SAXException {
if("yweather:condition".equals(qName)) {
String s_date = attributes.getValue(3);
try {
Date publish = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm a z",

Locale.US).parse(s_date);
//System.out.println("Publish: " + publish.toString());
}
catch (Exception e) {
e.printStackTrace();
throw new SAXException("Cannot parse date: " + s_date);
}
}
else if("yweather:forecast".equals(qName)) {
String s_date = attributes.getValue(1);
Date date = null;
try {
date = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(s_date);
}
catch (Exception e) {
e.printStackTrace();
throw new SAXException("Cannot parse date: " + s_date);
}
int low = Integer.parseInt(attributes.getValue(2));
int high = Integer.parseInt(attributes.getValue(3));
String text = attributes.getValue(4);
int code = Integer.parseInt(attributes.getValue(5));
System.out.println("Weather: "+ text + ", low=" + low + ", high=" + high);
}
super.startElement(uri, localName, qName, attributes);
}
}

  運行結果:

  Weather: Partly Cloudy, low=7, high=16
  Weather: Sunny, low=7, high=20

  Yahoo會返回當天和第二天的Weather預報。

熱門評論
最新評論
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數: 0/500 (您的評論需要經過審核才能顯示)
主站蜘蛛池模板: 成年人网站在线免费观看 | av日韩精品 | 亚洲天堂男人天堂 | 国产91免费视频 | 日韩高清精品免费观看 | 深夜视频在线观看 | 欧美成人久久 | 免费a在线| 日韩精品视频免费在线观看 | 午夜久久久久久 | 色婷五月天| 亚洲男人天堂网 | 日韩色网站 | 国产午夜精品一区二区三区视频 | 日本美女黄色 | 国产精品成人免费视频 | 久久久久亚洲 | 中文字幕丰满人伦在线 | 成人精品一区二区三区 | 欧美黄色一级视频 | 亚洲精品蜜桃 | 久久久三级 | 日韩视频专区 | 日韩精品视频在线播放 | 一级做a视频 | 中文字幕日韩高清 | 99国产免费 | 国内精品国产成人国产三级 | 国产成人在线视频 | 欧美黄网站 | 大桥未久在线视频 | 亚洲精品国产一区 | 色婷婷狠狠| 中文字幕永久 | 成人黄色在线 | 久久久网站 | 久久国产综合 | 午夜免费在线 | 欧美aaaaa| 大尺度做爰呻吟舌吻网站 | 一级黄色免费视频 |