距离第一章的入门讲解已经有一段时间了,最近的确没有空,今天我们在上一次的基础上来进行一个扩展,实现天气查询功能,大概就是,我们在手机上想我们的公众账号发送一个消息,比如“weather”,服务器获取当前天气进行返回给手机客户端,(大家可以关注我的公众账号测试效果,左边个人资料有二维码。注*此功能已经在第三章《结合百度云平台和微信公众平台开发(三)》升级,取消了Weather关键字,改为“天气北京”,“天气南京”类的定点查询)如下图:
首先要声明的是,天气的获取有很多种方法,但我们最好是使用WebXml这个网站提供我们的数据,更新快,不涉及侵权,非常好用的地址是:点击打开链接
入口类没有什么变化,就是多了一个字符判断而已,这里就不贴完整代码了,大家可以参考上一篇:
//微信客户端发送time,服务器解析到后返回当前时间
else if(keyword.equals("time")){
contentStr = GetTime.getTime();
}else if(keyword.equals("weather")){
contentStr = GetTime.getTime()+"~~"+Weather.getWeather();
}
下面说说获取天气的代码实现:
package com.weixin.zfeng.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Weather {
private static String SERVICES_HOST = "www.webxml.com.cn";
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";
/**
* 城市代码 / 镇江: 1954
*/
//这里是通过城市对应的代码来实现查询的,全国各个城市代码号,大家可以在官方网站看到,实际开发是,需要将它们全部拿来
private static int CITICODE = 1954;
public static String getWeather(){
String desc = "今天是" + DateUtils.getYear() + ","
+ DateUtils.getWeekOfDate(new Date());
desc += new Weather().getWeatherStr();
return desc;
}
public InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
URLConnection urlConn = urlObj.openConnection();
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
public String getWeatherStr() {
String desc = "";
try {
List<String> weatherList = getWeather(CITICODE);
if (weatherList != null && weatherList.size() > 7) {
String tianqi = weatherList.get(7);
if (tianqi.contains("日")) {
tianqi = tianqi.substring(tianqi.indexOf("日") + 1);
}
String wendu = weatherList.get(8);
desc += ",天气" + tianqi;
desc += " ,";
desc += wendu.replace("℃", "度").replace("/", "--");
}
} catch (Exception e) {
e.printStackTrace();
return desc;
}
return desc;
}
public List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory
.newInstance();
documentBF.setNamespaceAware(true);
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL
+ cityCode);
document = documentB.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
inputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return weatherList;
}
//大家务必在此测试一下后哦
// public static void main(String[] args) {
// System.out.println(Weather.getWeather());
// }
}
就这么多,很简单的,上一次我忘记在入口类中处理编码问题,这样汉字会出现乱码,大家记住改一下:
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
文章转载须注明出处:
http://blog.csdn.net/fengfeng91