精品久久看,欧美成人久久一级c片免费,日本加勒比在线精品视频,国产一区二区三区免费大片天美,国产成人精品999在线,97理论三级九七午夜在线观看

當前位置:首頁文章首頁 IT學院 IT技術

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

作者:  來源:  發布時間:2011-6-24 11:55:29  點擊:

下面提供給分享學習的關于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預報。

相關軟件

相關文章

文章評論

軟件按字母排列: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
主站蜘蛛池模板: 国产成人综合一区精品 | 日本免费a视频 | 四虎www.| 国产成人毛片精品不卡在线 | 99久久综合狠狠综合久久男同 | 色激情综合网 | 特级一级毛片免费看 | 国产不卡精品一区二区三区 | 国产欧美精品一区aⅴ影院 国产欧美精品午夜在线播放 | 三级成人影院 | 全部免费a级毛片 | 国产精品视频国产永久视频 | 欧美一区二区自偷自拍视频 | 国产成人精品实拍在线 | 涩涩片影院 | 欧美h网| 久久精品a亚洲国产v高清不卡 | 激情天堂 | 精品视频一二三区 | 六月丁香啪啪 | 成人小视频免费在线观看 | 国产中文字幕视频 | 五月综合色| 国产精品亚洲片在线牛牛影视 | 日本男人天堂 | 成 人 黄 色 视频免费播放 | 亚洲综合久久久久久中文字幕 | 久久综合综合久久97色 | 国产黄色大全 | 国内精品一区二区三区 | 久久狠狠一本精品综合网 | 欧美一区a | 国产真实老女人 | 你懂的视频网 | 国产精品亚洲片在线牛牛影视 | 国产高清在线观看视频手机版 | 成av人电影在线观看 | 精品视频一区在线观看 | 欧美色性视频 | 男人天堂社区 | 精品一区二区三区四区五区六区 |