转自新浪sae论坛,都是我自己写的,所以在这里也属于原创。http://cloudbbs.org/forum.php?mod=viewthread&tid=23175
要使用百度天气预报api,首先要有密钥ak,申请地址http://developer.baidu.com/map/lbs-cloud.htm
然后向http://api.map.baidu.com/telematics/v3/weather?location=郑州&output=xml&ak=A72e372de05e63c8740b2622d0ed8ab1
请求,然后会返还给你一个xml文档格式的天气预报如下:
-
<?xml version="1.0" encoding="utf-8" ?>
-
<CityWeatherResponse>
-
<status>success</status>
-
<date>2014-04-29</date>
-
<results>
-
<currentCity>郑州</currentCity>
-
<weather_data>
-
<date>周二(今天, 实时:20℃)</date>
-
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png<;/dayPictureUrl>
-
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png<;/nightPictureUrl>
-
<weather>晴</weather>
-
<wind>微风</wind>
-
<temperature>13℃</temperature>
-
<date>周三</date>
-
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png<;/dayPictureUrl>
-
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png<;/nightPictureUrl>
-
<weather>晴</weather>
-
<wind>微风</wind>
-
<temperature>28 ~ 15℃</temperature>
-
<date>周四</date>
-
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png<;/dayPictureUrl>
-
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/yin.png<;/nightPictureUrl>
-
<weather>多云转阴</weather>
-
<wind>微风</wind>
-
<temperature>30 ~ 15℃</temperature>
-
<date>周五</date>
-
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/yin.png<;/dayPictureUrl>
-
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png<;/nightPictureUrl>
-
<weather>阴转多云</weather>
-
<wind>微风</wind>
-
<temperature>25 ~ 15℃</temperature>
-
</weather_data>
-
</results>
-
</CityWeatherResponse>
接下 来只需要对此xml文档解析取出数据即可
具体代码如下:
测试结果:
2014-04-29
周二(今天, 实时:20℃)===晴===微风===13℃
周三===晴===微风===28 ~ 15℃
周四===多云转阴===微风===30 ~ 15℃
周五===阴转多云===微风===25 ~ 15℃
|