微信公众平台开发学习笔记·二

经过上次的搭建后,自己尝试做了一个天气的应答程序。目的主要是测试实时信息的应答。主要用到了json和调用其余网站接口的知识,而在调试的时候,主要用到了Log4j的知识。

        

<pre name="code" class="java">public static String getWeatherInfo(String content){
		WeatherUtil.fillCityMap();
		String weatherInfo = "";
		String cityName = content.substring(0, content.indexOf("天"));
		String cityCode = cityMap.get(cityName);
		if (logger.isDebugEnabled()){
			logger.debug("城市代码为:"+cityCode);
		}
		String requestURL = "http://m.weather.com.cn/atad/";
		requestURL += cityCode;
		requestURL += ".html";
		String jsonResult = HttpURLConnectionUtil.httpRequest(requestURL);
		if (logger.isDebugEnabled()){
			logger.debug("jsonResult:"+jsonResult);
		}
		JSONObject jsonObject = JSONObject.fromObject(jsonResult);
		if (logger.isDebugEnabled()){
			logger.debug("jsonObject:"+jsonObject);
		}
		WeatherInfo weatherInfoResult = (WeatherInfo) JSONObject.toBean(jsonObject.getJSONObject("weatherinfo"), WeatherInfo.class);
		if (logger.isDebugEnabled()){
			logger.debug("weatherInfoResult:"+weatherInfoResult.toString());
		}
		weatherInfo = packageWeatherInfo(weatherInfoResult);
		return weatherInfo;
	}

public class HttpURLConnectionUtil {
	static Logger logger = Logger.getLogger(HttpURLConnectionUtil.class);
	
	public static String httpRequest(String requestURL){
		StringBuffer buffer = new StringBuffer();  
        try {  
            URL url = new URL(requestURL);  
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();  
  
            httpUrlConn.setDoOutput(false);  
            httpUrlConn.setDoInput(true);  
            httpUrlConn.setUseCaches(false);  
  
            httpUrlConn.setRequestMethod("GET");  
            httpUrlConn.connect();  
            if (logger.isDebugEnabled()){
    			logger.debug("11111");
    		}
            // 将返回的输入流转换成字符串  
            InputStream inputStream = httpUrlConn.getInputStream();  
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
            String str = null;  
            while ((str = bufferedReader.readLine()) != null) {  
                buffer.append(str);  
            }  
            bufferedReader.close();  
            inputStreamReader.close();  
            // 释放资源  
            inputStream.close();  
            inputStream = null;  
            httpUrlConn.disconnect();  
        } catch (Exception e) {  
        }  
        return buffer.toString();  
	}
}
</pre><p>需要注意的是这里调用的国家气象局的接口获取天气数据,返回的其实是一个json的数组,要通过jsonObject.getJSONObject("weatherinfo")才能获得jsonObject。而HttpURLConnectionUtil则是一个向别的网站发请求的固定写法,通过url地址开启一个connection,然后将返回值转换成字符串。接下来是log4j的配置。这里主要是为了调试程序的时候,借用日志功能来调试。</p><pre name="code" class="java">log4j.rootCategory=DEBUG, stdout   
   
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
log4j.appender.stdout.layout.ConversionPattern = [%p] <%d{yyyy-MM-dd HH:mm:ss:SSS}> %C{1}::%M: %m%n
  
log4j.logger.com.wechat.logiccode=DEBUG   
</pre><p>这里的配置非常简单,意思就是打debug级别的日志,输出到控制台上,并且将com.wechat.logiccode包名下的类指定为debug级别。然后我们在公众号上输入城市天气后,就会回复给我们天气信息了。</p><p></p><pre>
</pre><pre>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值