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

當前位置: 首頁IT技術(shù) → Yahoo Service實現(xiàn)天氣預報的實例代碼分享

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

更多

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

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

  尋遍了國內(nèi)所有的氣象站點,沒找見提供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);
}
}

  運行結(jié)果:

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

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

熱門評論
最新評論
發(fā)表評論 查看所有評論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
主站蜘蛛池模板: 九草在线播放 | 97人洗澡人人澡人人爽 | 亚洲高清在线视频 | 国产激情在线 | 国产亚洲欧美成人久久片 | 激情综合六月 | 久久精品视频网站 | 久久青青成人亚洲精品 | 黄色福利视频网站 | 亚洲国产午夜精品理论片的软件 | 无码免费一区二区三区免费播放 | 91免费视频观看 | 男女性免费视频观看 | 亚洲不卡免费视频 | 农村黄a三级三级三级 | 美女日批视频在线观看 | 日韩性网 | 国产免费观看嫩草影院 | 成年人免费观看的视频 | 午夜a级毛片 | 主播蜜汁丝袜 精品自拍 | 亚洲 欧美 中文字幕 | 欧美亚洲另类色国产综合 | 日本中文字幕一区二区有码在线 | 91热久久免费频精品动漫99 | 色五夜| 欧美一级手机免费观看片 | aⅴ在线免费观看 | 国产精品久久久久久影视 | 日韩欧美视频二区 | 成人免费午间影院在线观看 | 国产制服在线 | 午夜日本| free性欧美淫俄罗斯 | 久草最新在线 | 黄色一级一级 | 免费午夜视频 | 国产精品系列在线一区 | 91成人免费观看 | 日本三级视频在线 | 曰批女人视频在线观看 |