变态重口极致另类在线-波多久久夜色精品国产-波多野结衣在线观看一区-波多野结衣在线观看一区二区-污污的网站免费阅读-污污视频网址

當前位置:首頁文章首頁 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
主站蜘蛛池模板: 影音先锋激情 | 在线免费中文字幕 | 偷亚洲偷国产欧美高清 | 嫩草在线视频www免费观看 | 91中文字幕网 | 免费看黄色片网站 | 激情五月视频 | 中国一级毛片免费看视频 | 草草免费视频 | 一级毛片在线完整免费观看 | 天天操夜夜摸 | 久久免费视频7 | 国产成人亚洲精品影院 | 99久久999久久久综合精品涩 | 国产精品一区二区不卡 | 福利在线影院 | 国产福利毛片 | ppypp日本欧美一区二区 | 成人永久免费视频 | 久久精品视频6 | 天天狠狠干 | ccav成人| 日韩亚洲一区中文字幕在线 | 91香蕉视频苹果 | 簧片在线免费观看 | 国产黄三级三·级三级 | 亚洲高清成人欧美动作片 | 大又黄又粗又爽妇女毛片 | freexxxx性大陆另类 | 一本无线乱码不卡一二三四 | 操亚洲美女| 欧美黑人巨大日本人又爽又色 | 人人澡人人澡 | 国产在线观看不卡免费高清 | 天天做天天爱天天操 | 狠狠干婷婷 | 国产一区二区三区四 | 韩国xxxx色视频在线观看 | 国产日韩视频在线观看 | 在线视频精品免费 | 免费一看一级毛片人 |