首页 > 代码库 > 微信百度天气查询

微信百度天气查询

想在微信上做个天气预报查询功能,发现百度api还挺好用的。简单介绍下

/**
 * WxDaoImpl
 * 
 * @author xuyw
 * @email xyw10000@163.com
 * @date 2014-06-22
 */

  1. 1.接口说明
  2. 2.接口示例
  3. 3.接口参数说明
  4. 4.返回结果
  5. 5.返回xml格式的数据
  6. 6.返回json格式的数据

接口说明

根据经纬度/城市名查询天气的结果

接口示例

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey

接口参数说明

参数类型参数名称是否必须具体描述
Stringaktrue开发者密钥
Stringsnfalse若用户所用ak的校验方式为sn校验时该参数必须。 (sn生成算法)
Stringlocationtrue输入城市名或经纬度,城市名称如:北京,经纬度格式为lng,lat坐标如: location=116.305145,39.982368;城市天气预报中间"|"分隔,location=116.305145,39.982368| 122.305145,36.982368|….
Stringoutputfalse输出的数据格式,默认为xml格式,当output设置为’json’时,输出的为json格式的数据;
Stringcoord_typefalse请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标。wgs84表示gps获取的坐标。

返回结果

参数名称含义说明
currentCity当前城市返回城市名
status返回结果状态信息 
date当前时间年-月-日
results天气预报信息白天可返回近期3天的天气情况(今天、明天、后天)、晚上可返回近期4天的天气情况(今天、明天、后天、大后天)
results.currentCity当前城市 
results.weather_dataweather_data.date天气预报时间 
weather_data.dayPictureUrl白天的天气预报图片url 
weather_data.nightPictureUrl晚上的天气预报图片url 
weather_data.weather天气状况所有天气情况(”|”分隔符):晴|多云|阴|阵雨|雷阵雨|雷阵雨伴有冰雹|雨夹雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|阵雪|小雪|中雪|大雪|暴雪|雾|冻雨|沙尘暴|小雨转中雨|中雨转大雨|大雨转暴雨|暴雨转大暴雨|大暴雨转特大暴雨|小雪转中雪|中雪转大雪|大雪转暴雪|浮尘|扬沙|强沙尘暴|霾
weather_data.wind风力 
weather_data.temperature温度 

返回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>
</result>
</results>
</CityWeatherResponse>

返回json格式的数据

{
error: 0,
status: "success",
date: "2013-07-17",
results: 
[
{
currentCity: "北京市",
weather_data: 
[
{
date: "周三(今天, 实时:24℃)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "23℃~ 15℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/zhongyu.png",
weather: "雷阵雨转中雨",
wind: "微风",
temperature: "29~22℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/yin.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "阴转多云",
wind: "微风",
temperature: "31~23℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "31~24℃"
}
]
},
{
currentCity: "合肥市",
weather_data: 
[
{
date: "今天(周三)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风3-4级",
temperature: "27℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东北风3-4级",
temperature: "35~27℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "南风",
temperature: "35~27℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风",
temperature: "34~27℃"
}
]
}
]
}
编写代码

 /**
     * 获取天气
     * @param city
     * @return
     */
	public static JSONObject getWeatherInfo(String city) {
		JSONObject obj = JSONObject.fromObject(HttpUtil.getRequest(
				GET_WEATHER_URL, "location:" + city, "output:json", "ak:"
						+ Config.BAIDU_GEOCONV_KEY));
		return obj;
	}

返回结果

{"error":0,"status":"success","date":"2014-06-23","results":[{"currentCity":"乐平","weather_data":[{"date":"周一 06月23日 (实时:25℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"阵雨","wind":"微风","temperature":"26 ~ 22℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"阵雨","wind":"微风","temperature":"25 ~ 22℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/leizhenyu.png","weather":"阵雨转雷阵雨","wind":"微风","temperature":"27 ~ 23℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/leizhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"雷阵雨转阵雨","wind":"微风","temperature":"29 ~ 23℃"}]}]}

将结果转换为微信多图文

	/**
	 * 获取天气
	 * 
	 * @param map
	 * @return
	 */
	public String getWeather(Map map) {
		String message=null;
		String openid = map.get("FromUserName") + "";
		String toUserName = map.get("ToUserName") + "";
		String city = map.get("city") + "";
		JSONObject obj = BaiduUtil.getWeatherInfo(city);
		String status = obj.getString("status");
		if("success".equals(status)){//查找到了
			JSONArray jarray=obj.getJSONArray("results");
			int size=jarray.size();
			if(size>0){
				JSONObject j2=jarray.getJSONObject(0);
				JSONArray jarray2=j2.getJSONArray("weather_data");
				List<Article> articleList = new ArrayList<Article>();
				// 创建图文消息
				NewsMessage newsMessage = new NewsMessage();
				newsMessage.setToUserName(openid);
				newsMessage.setFromUserName(toUserName);
				newsMessage.setCreateTime(new Date().getTime());
				newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
				newsMessage.setFuncFlag(0);
				int size2=jarray2.size();
				for (int i = 0; i < jarray2.size(); i++) {
					JSONObject j3=jarray2.getJSONObject(i);
					String date=j3.getString("date");
					String weather=j3.getString("weather");
					String wind=j3.getString("wind");
					String temperature=j3.getString("temperature");
					String title="";
					String img_url=j3.getString("dayPictureUrl");
					if(i==0){
						title=date+" "+weather+" "+wind;
					}else{
						title=date+" "+weather+" "+temperature+" "+wind;
					}
					Article article = new Article();
					article.setTitle(title);
					article.setDescription("");
					article.setPicUrl(img_url);
					article.setUrl("http://blog.csdn.net/xuyw10000");
					articleList.add(article);
				}
				newsMessage.setArticleCount(size2);
				newsMessage.setArticles(articleList);
				message = MessageUtil.newsMessageToXml(newsMessage);
			}else{
				map.put("xuywMsg", "啊噢,找不到“"+city+"”的天气。");
				message=sendTextMessage(map);
			}
		}else{//无结果
			map.put("xuywMsg", "啊噢,这个地方百度都找不到。");
			message=sendTextMessage(map);
		}
		return message;
	}

结果

<xml>
  <ToUserName><![CDATA[sadsdaewwaewea]]></ToUserName>
  <FromUserName><![CDATA[bbbbb]]></FromUserName>
  <CreateTime><![CDATA[1403495665819]]></CreateTime>
  <MsgType><![CDATA[news]]></MsgType>
  <FuncFlag><![CDATA[0]]></FuncFlag>
  <ArticleCount><![CDATA[4]]></ArticleCount>
  <Articles>
    <item>
      <Title><![CDATA[周一 06月23日 (实时:25℃) 阵雨 微风]]></Title>
      <Description><![CDATA[]]></Description>
      <PicUrl><![CDATA[http://api.map.baidu.com/images/weather/day/zhenyu.png]]></PicUrl>
      <Url><![CDATA[http://blog.csdn.net/xuyw10000]]></Url>
    </item>
    <item>
      <Title><![CDATA[周二 阵雨 25 ~ 22℃ 微风]]></Title>
      <Description><![CDATA[]]></Description>
      <PicUrl><![CDATA[http://api.map.baidu.com/images/weather/day/zhenyu.png]]></PicUrl>
      <Url><![CDATA[http://blog.csdn.net/xuyw10000]]></Url>
    </item>
    <item>
      <Title><![CDATA[周三 阵雨转雷阵雨 27 ~ 23℃ 微风]]></Title>
      <Description><![CDATA[]]></Description>
      <PicUrl><![CDATA[http://api.map.baidu.com/images/weather/day/zhenyu.png]]></PicUrl>
      <Url><![CDATA[http://blog.csdn.net/xuyw10000]]></Url>
    </item>
    <item>
      <Title><![CDATA[周四 雷阵雨转阵雨 29 ~ 23℃ 微风]]></Title>
      <Description><![CDATA[]]></Description>
      <PicUrl><![CDATA[http://api.map.baidu.com/images/weather/day/leizhenyu.png]]></PicUrl>
      <Url><![CDATA[http://blog.csdn.net/xuyw10000]]></Url>
    </item>
  </Articles>
</xml>
HttpUtil 见 http://blog.csdn.net/xuyw10000/article/details/33342489

部署sae后运行