原文:http://blog.csdn.net/pamchen/article/details/38718947
说明:
本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
- package demo.servlet;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import demo.process.WechatProcess;
- public class WechatServlet extends HttpServlet {
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
-
-
- StringBuffer sb = new StringBuffer();
- InputStream is = request.getInputStream();
- InputStreamReader isr = new InputStreamReader(is, "UTF-8");
- BufferedReader br = new BufferedReader(isr);
- String s = "";
- while ((s = br.readLine()) != null) {
- sb.append(s);
- }
- String xml = sb.toString();
-
- String result = "";
-
- String echostr = request.getParameter("echostr");
- if (echostr != null && echostr.length() > 1) {
- result = echostr;
- } else {
-
- result = new WechatProcess().processWechatMag(xml);
- }
-
- try {
- OutputStream os = response.getOutputStream();
- os.write(result.getBytes("UTF-8"));
- os.flush();
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>WechatServlet</servlet-name>
- <servlet-class>demo.servlet.WechatServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>WechatServlet</servlet-name>
- <url-pattern>/wechat.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。
下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。
- package demo.process;
-
- import java.util.Date;
-
- import demo.entity.ReceiveXmlEntity;
-
- public class WechatProcess {
-
- public String processWechatMag(String xml){
-
- ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(xml);
-
-
- String result = "";
- if("text".endsWith(xmlEntity.getMsgType())){
- result = new TulingApiProcess().getTulingResult(xmlEntity.getContent());
- }
-
-
- result = new FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(), xmlEntity.getToUserName(), result);
-
- return result;
- }
- }
2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:
- package demo.process;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.Iterator;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
-
- import demo.entity.ReceiveXmlEntity;
- public class ReceiveXmlProcess {
-
- public ReceiveXmlEntity getMsgEntity(String strXml){
- ReceiveXmlEntity msg = null;
- try {
- if (strXml.length() <= 0 || strXml == null)
- return null;
-
-
- Document document = DocumentHelper.parseText(strXml);
-
- Element root = document.getRootElement();
-
- Iterator<?> iter = root.elementIterator();
-
-
- msg = new ReceiveXmlEntity();
-
-
- Class<?> c = Class.forName("demo.entity.ReceiveXmlEntity");
- msg = (ReceiveXmlEntity)c.newInstance();
-
- while(iter.hasNext()){
- Element ele = (Element)iter.next();
-
- Field field = c.getDeclaredField(ele.getName());
-
- Method method = c.getDeclaredMethod("set"+ele.getName(), field.getType());
-
- method.invoke(msg, ele.getText());
- }
- } catch (Exception e) {
-
- System.out.println("xml 格式异常: "+ strXml);
- e.printStackTrace();
- }
- return msg;
- }
- }
2.3 调用图灵机器人api接口,获取智能回复内容:
- package demo.process;
-
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
-
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONException;
- import org.json.JSONObject;
-
- public class TulingApiProcess {
-
- public String getTulingResult(String content){
-
- String apiUrl = "http://www.tuling123.com/openapi/api?key=11111111&info=";
- String param = "";
- try {
- param = apiUrl+URLEncoder.encode(content,"utf-8");
- } catch (UnsupportedEncodingException e1) {
-
- e1.printStackTrace();
- }
-
-
- HttpGet request = new HttpGet(param);
- String result = "";
- try {
- HttpResponse response = HttpClients.createDefault().execute(request);
- if(response.getStatusLine().getStatusCode()==200){
- result = EntityUtils.toString(response.getEntity());
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- if(null==result){
- return "对不起,你说的话真是太高深了……";
- }
-
- try {
- JSONObject json = new JSONObject(result);
-
- if(100000==json.getInt("code")){
- result = json.getString("text");
- }
- } catch (JSONException e) {
-
- e.printStackTrace();
- }
- return result;
- }
- }
2.4 将结果封装为微信规定的xml格式,并返回给1.1中创建的servlet接口。
- package demo.process;
-
- import java.util.Date;
- public class FormatXmlProcess {
-
- public String formatXmlAnswer(String to, String from, String content) {
- StringBuffer sb = new StringBuffer();
- Date date = new Date();
- sb.append("<xml><ToUserName><![CDATA[");
- sb.append(to);
- sb.append("]]></ToUserName><FromUserName><![CDATA[");
- sb.append(from);
- sb.append("]]></FromUserName><CreateTime>");
- sb.append(date.getTime());
- sb.append("</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[");
- sb.append(content);
- sb.append("]]></Content><FuncFlag>0</FuncFlag></xml>");
- return sb.toString();
- }
- }
总结,以上便是微信公众平台开发的全部流程,整体来看并不复杂,要非常感谢图灵机器人提供的api接口,帮我们解决了智能回复这一高难度问题。其他类型的消息处理与示例中类似,有兴趣的开发者可以联系我进行交流学习,希望本文对大家有所帮助。
本问中的代码示例已经上传到了csdn的个人资源中,有需要的可以去下载:http://download.csdn.net/detail/pamchen/7793979
微信公众平台java开发详解(工程代码+解析)
原文:http://www.cnblogs.com/tc310/p/4644216.html