结合百度云平台和微信公众平台开发(三)

时间:2022-09-22 19:29:38

            这次我按照柳峰前辈的建议,天气数据查询来源于“中国气象网”,第三篇那个只能算一个Demo,这次我们将对这部分功能完整化。描述:发送“天气绵阳”,“天气北京”,系统就会自动返回当天当地的最新天气情况,以及提醒和注意,那么我们就先看看效果吧关注个人信息栏公众号马上就可以看效果啊

结合百度云平台和微信公众平台开发(三)

文章及代码为作者原创,转载须注明原文出处:http://blog.csdn.net/fengfeng91/article/details/17660887

写在前面的话:

    其实在完成这个功能之前,我被“逼了”整整一天,好不容易搞来个中国气象网的城市id对照的xml文件吧,自己就想当然的用Dom4j去解析查询(当时自己也知道这样的话比较耗时,耗性能,因为xml文件足足有3000多行),好吧,一阵摆弄之后,本地测试,嗯,没有一点问题,数据都打印出来了。感觉还不错。满怀期待的发布发哦百度云上去后。发送:”天气绵阳“,~~~返回,返回个毛啊,完全没反应啊,于是我到处找问题,字节流中文乱码处理啊等等~来来回回20多次吧,都快发疯了,最后出去透了透气,回来后这样想想:“是不是因为我的偷懒导致返回时间大于了5秒?!所以没有返回信息。”,于是就马上将xml文件里的三种信息用代码提取出来,写一个类CityDatasArray.java,里面封装3个数组将信息一一录入~~~~本地测试后,明显感觉速度至少提升了一倍以上,抱着60%的心态又一次发布,发送“天气绵阳”,“天气北京”~~一一成功!说了这些,都是教训啊,以后要以更严谨的态度对待编程啊!结合百度云平台和微信公众平台开发(三)


那么那个几千行的城市id对照表我就不贴出来了吧,我这里给大家提供一个我写好的java文件。点击下载


天气数据获取,解析,组装类:GetWeatherById.java

package com.weixin.zfeng.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GetWeatherByCity {
	//天气数据来自中国气象网
	private static String httpUrl = "http://m.weather.com.cn/data/";
	/**
	 * 请求获取Json字符串
	 * @param cityid 城市代码
	 * @return
	 */
	private static String getWeatherFile(String cityid){  
          
        HttpURLConnection connection = null;  
        InputStream is = null;  
        BufferedInputStream bis = null;  
          
        StringBuffer sb = new StringBuffer();  
        try {  
            URL imgUrl = new URL(httpUrl+cityid+".html");  
            connection = (HttpURLConnection) imgUrl.openConnection();  
            connection.setRequestProperty("Accept-Charset","UTF-8");//解决中文乱码啊
            connection.setRequestProperty("Content-type","UTF-8");//解决中文乱码啊
            connection.setRequestProperty("contentType","UTF-8");//解决中文乱码啊
            connection.setConnectTimeout(5000);  
            connection.setRequestMethod("GET");  
            
            //输入流  
            is = connection.getInputStream();  
            //way1
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));  //解决中文乱码啊
            String line = "";  
            while ((line = in.readLine()) != null){  
              sb.append(line);  
            }  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (is != null) {  
                    is.close();  
                }  
                if (connection != null) {  
                    connection.disconnect();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return sb.toString();  
    }  
	/**
	 * 解析json数据
	 * @param cityid
	 * @return
	 */
	public static String getWeatherMsg(String cityid){
		StringBuilder info = new StringBuilder();
		JsonParser jp = new JsonParser();
		JsonElement jse = jp.parse(GetWeatherByCity.getWeatherFile(cityid));
		JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
		String updTime = jso.get("fchh").getAsString();
		if(updTime != null){
				//基本信息
				String today_basic = jso.get("city").getAsString()+"\n"+jso.get("date_y").getAsString()+"  "+jso.get("week").getAsString();//今天
				//温度
				String today_temperature = jso.get("temp1").getAsString();//今天
				//天气
				String today_weather = jso.get("weather1").getAsString();//今天
				//风向风力
				String today_wind = jso.get("wind1").getAsString();//今天
				//风速级别描述
				String today_wind_num = jso.get("fl1").getAsString();//今天
				//今天穿衣指数
				String today_clothes = jso.get("index_d").getAsString();//今天
				//紫外线
				String today_light = jso.get("index_uv").getAsString();//今天
				//洗车
				String today_wash_car = jso.get("index_xc").getAsString();//今天
				//旅游 
				String today_tour = jso.get("index_tr").getAsString();//今天
				//舒适指数
				String today_easy_num = jso.get("index_co").getAsString();//今天
				//晨练
				String today_morning_exercise = jso.get("index_cl").getAsString();//今天
				//晾晒
				String today_hang_out = jso.get("index_ls").getAsString();//今天
				//过敏
				String today_allergy = jso.get("index_ag").getAsString();//今天
				
				info.
				append("城市: ").append(today_basic).append("\n").
				append("温度: ").append(today_temperature).append("\n").
				append("天气: ").append(today_weather).append("\n").
				append("风向: ").append(today_wind).append("\n").
				append("风速: ").append(today_wind_num).append("\n").
				append("穿衣指数: ").append(today_clothes).append("\n").
				append("紫外线: ").append(today_light).append("\n").
				append("洗车: ").append(today_wash_car).append("\n").
				append("旅游 : ").append(today_tour).append("\n").
				append("舒适指数: ").append(today_easy_num).append("\n").
				append("晨练: ").append(today_morning_exercise).append("\n").
				append("晾晒: ").append(today_hang_out).append("\n").
				append("过敏: ").append(today_allergy).append("\n");
		}
		return info.toString();
	}
}

组装后格式:

城市: 绵阳
2013年12月29日  星期日
温度: 10℃~1℃
天气: 多云
风向: 旋转风小于3级
风速: 小于3级
穿衣指数: 建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。
紫外线: 最弱
洗车: 适宜
旅游 : 适宜
舒适指数: 较舒适
晨练: 较适宜
晾晒: 基本适宜
过敏: 极不易发

入口类:MainServer.java消息处理部分代码:

//微信客户端发送"天气+地点",服务器解析到后返回当前时间
    else if(sourseMsg.startsWith("天气")){
    	//解析城市xml获得城市对应代码
    	city_id = CityDatasArray.getTheCityId(sourseMsg.substring(2));//去掉前两个字符,取后部分
    	//根据城市代码查询天气详情
    	return GetWeatherByCity.getWeatherMsg(city_id);
	}  
其余部分代码请大家参考前面的章节,基本没有变化。暂且不给大家工程源码,希望大家多思考研究啊!

谢谢!结合百度云平台和微信公众平台开发(三)
文章及代码为作者原创,转载须注明原文出处:http://blog.csdn.net/fengfeng91/article/details/17660887


结合百度云平台和微信公众平台开发(三)