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

當前位置:首頁文章首頁 IT學院 IT技術(shù)

Yahoo Service實現(xiàn)天氣預(yù)報的實例代碼分享

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

下面提供給分享學習的關(guān)于Yahoo Service實現(xiàn)天氣預(yù)報的實例代碼,希望能夠給大家?guī)韼椭騿l(fā)。

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

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

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

  以北京為例,在weather.yahoo.com查找北京的城市代碼為CHXX0008,對應(yīng)的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來響應(yīng)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);
}
}

  運行結(jié)果:

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

  Yahoo會返回當天和第二天的Weather預(yù)報。

相關(guān)軟件

相關(guān)文章

文章評論

軟件按字母排列: 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
主站蜘蛛池模板: 久久久久黄色 | 久久精品福利视频 | 在线免费毛片 | 精品视频一区二区 | 免费国产一区二区 | 国产黄色在线观看 | 国产一级黄色大片 | 日韩天堂在线 | 青娱乐福利视频 | 国产精品成人国产乱一区 | 午夜黄色大片 | av大全在线观看 | 久久久久久一区 | 九色精品| 日韩免费在线观看 | 国产欧美一区二区三区在线看蜜臀 | 日韩欧美精品 | 国产三级在线播放 | 懂色av懂色av粉嫩av | 免费理论片 | 欧美国产在线观看 | 成人在线小视频 | 特黄一级毛片 | 91久| av免费看网站 | 久久久久久久影院 | 爱搞逼综合网 | 国产小精品| 青青青国产| brazzers疯狂作爱 | av福利在线观看 | 中文字幕av一区二区三区谷原希美 | 日本少妇中文字幕 | 依人在线 | 黄色成人av | 中文字幕2021 | 黄色一级大片在线免费看国产一 | 欧美精品久久久久久久 | 国产一区欧美 | 国产黄色一区二区 | 一区二区免费 |