您的位置:首页 > 移动开发 > 微信开发

微信开发二三事:功能应用一、天气预报

2015-04-09 18:36 483 查看
好吧,又断更了几天......

前段时间,12班的同学学到了xml解析,其中的一个作业就是用swt+xml去写一个天气预报。其实,只要是看了微信开发者文档的同学应该都清楚,微信基本上是基于xml去传输数据的,所以学好xml解析,那么微信开发起来就不会太困难。

OK,言归正传,如何获取到天气预报。这里我用的不是webxml里面的接口了,因为太坑....我用的百度车联网的api,网址:http://developer.baidu.com/map/index.php?title=car/api/weather。因为百度车联网的api每天可以免费使用5000次,一个ak,实在不够的话,就去多申请几个ak,放到数组里面循环使用。

首先,我们来看看接口:http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey。这里有几个主要的参数。

1、location:你要查询天气的城市

2、output=json:以json数据格式返回,当然,这里还可以用xml数据返回:output=xml

3、ak:应用密匙。这个ak是需要创建应用,应用类型为浏览器端才有的ak。

然后百度给我们列出了一段xml格式的返回结果,如下:
<CityWeatherResponse>
<status>success</status>
<date>2013-07-18</date>
<results>
<currentCity>北京市</currentCity>
<weather_data>
<date>周三(今天, 实时:24℃)</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>23℃~ 15℃</temperature>
<date>周四</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/leizhenyu.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/zhongyu.png </nightPictureUrl>
<weather>雷阵雨转中雨</weather>
<wind>微风</wind>
<temperature>29~22℃</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>31~23℃</temperature>
<date>周六</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>31~24℃</temperature>
</weather_data>
<currentCity>合肥市</currentCity>
<weather_data>
<date>周三</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>东风3-4级</wind>
<temperature>27℃</temperature>
<date>周四</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>东北风3-4级</wind>
<temperature>35~27℃</temperature>
<date>周五</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>南风</wind>
<temperature>35~27℃</temperature>
<date>周六</date>
<dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl>
<nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl>
<weather>多云</weather>
<wind>东风</wind>
<temperature>34~27℃</temperature>
</weather_data>
</results>
</CityWeatherResponse>
<weather>阵雨</weather>
<wind>东北风3-4级</wind>
<temperature>18℃</temperature>
<index>
<title>穿衣</title>
<zs>舒适</zs>
<tipt>穿衣指数</tipt>
<des>建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。</des>
<title>洗车</title>
<zs>不宜</zs>
<tipt>洗车指数</tipt>
<des>不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。</des>
<title>感冒</title>
<zs>较易发</zs>
<tipt>感冒指数</tipt>
<des>相对今天出现了较大幅度降温,较易发生感冒,体质较弱的朋友请注意适当防护。</des>
<title>运动</title>
<zs>较不宜</zs>
<tipt>运动指数</tipt>
<des>有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。</des>
<title>紫外线强度</title>
<zs>弱</zs>
<tipt>紫外线强度指数</tipt>
<des>紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。</des>
</index>
<pm25>166</pm25>
</results>
</CityWeatherResponse>

这个虽然看起来比较复杂,其实弄清楚几个关键节点就OK了,比如说温度、风力、时间、湿度、穿衣指数等等。

好,介绍到这里,接下来,我们看看到底怎么用,献上代码:

public String getWeather(String City) {
SAXReader reader = new SAXReader();
String result="";
try {
URL url = new URL(
"http://api.map.baidu.com/telematics/v3/weather?location="
+ City + "&output=xml&ak=你的ak");
URLConnection con = url.openConnection();// 通过在url上调用openConnection方法创建链接对象
con.connect();// 使用connect方法建立到远程对象的实际链接;

InputStream is = con.getInputStream();
Document doc=reader.read(is);
for(int i=1;i<=4;i++){
Node date=doc.selectSingleNode("//weather_data/date["+i+"]");//日期
Node weathers=doc.selectSingleNode("//weather_data/weather["+i+"]");//天气情况
Node wind=doc.selectSingleNode("//weather_data/wind["+i+"]");//风向
Node tem=doc.selectSingleNode("//weather_data/temperature["+i+"]");//温度
Node des=doc.selectSingleNode("//index/des[1]");//穿衣建议

if(i==1){
result=result+date.getStringValue()+"\n"+weathers.getStringValue()+"\n"+wind.getStringValue()+"\n"+tem.getStringValue()+"\n穿衣:"+des.getStringValue()+"\n\n\n";
}else{
result=result+date.getStringValue()+"\n"+weathers.getStringValue()+"\n"+wind.getStringValue()+"\n"+tem.getStringValue()+"\n\n\n";
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

} catch (DocumentException e) {
e.printStackTrace();
}
if(result==null||"".equals(result)){
result="网络错误";
}
return result;
}
那么现在天气预报已经得到,如何和微信联系起来呢?其实这一点,和微信上面的关键字回复差不多了,步骤如下:

1、我们暂时给天气预报定义一个关键字,为"tqyb"

2、要求用户发送tqyb+要查询的城市。

3、接收到用户发送来的信息,用split("\\+")截取,注意,这里需要用到转义

4、得到城市,调用天气预报的接口,返回天气预报

5、返回给用户

OK,以上就是微信获取天气预报,下次,我们来讲讲其他功能应用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐