博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信公众平台java开发具体解释(project代码+解析)
阅读量:7112 次
发布时间:2019-06-28

本文共 11665 字,大约阅读时间需要 38 分钟。

说明:
本次的教程主要是对微信公众平台开发人员模式的解说,网络上非常多类似文章,但非常多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行解说分析,让刚開始学习的人尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了这是一个自然语言解析的开放平台,能够帮我们解决整个微信开发过程中最困难的问题,此处不多讲,以下会有其具体的调用方式。

1.1 在登录微信官方平台之后,开启开发人员模式,此时须要我们填写url和token,所谓url就是我们自己server的接口,用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;/** * 微信服务端收发消息接口 *  * @author pamchen-1 *  */public class WechatServlet extends HttpServlet {	/**	 * The doGet method of the servlet. 
* * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); /** 读取接收到的xml消息 */ 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(); //次即为接收到微信端发送过来的xml数据 String result = ""; /** 推断是否是微信接入激活验证,仅仅有首次接入验证时才会收到echostr參数,此时须要把它直接返回 */ 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(); } } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

1.2 对应的web.xml配置信息例如以下,在生成WechatServlet.java的同一时候,可自己主动生成web.xml中的配置。前面所提到的url处能够填写比如:http;//server地址/项目名/wechat.do

This is the description of my J2EE component
This is the display name of my J2EE component
WechatServlet
demo.servlet.WechatServlet
WechatServlet
/wechat.do
index.jsp

1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发人员模式并成功接入、接收消息和发送消息这三个步骤。

以下就解说其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下总体流程处理代码,包含:xml数据处理、调用图灵api、封装返回的xml数据。
package demo.process;import java.util.Date;import demo.entity.ReceiveXmlEntity;/** * 微信xml消息处理流程逻辑类 * @author pamchen-1 * */public class WechatProcess {	/**	 * 解析处理xml、获取智能回复结果(通过图灵机器人api接口)	 * @param xml 接收到的微信数据	 * @return	终于的解析结果(xml格式数据)	 */	public String processWechatMag(String xml){		/** 解析xml数据 */		ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(xml);				/** 以文本消息为例,调用图灵机器人api接口,获取回复内容 */		String result = "";		if("text".endsWith(xmlEntity.getMsgType())){			result = new TulingApiProcess().getTulingResult(xmlEntity.getContent());		}				/** 此时,假设用户输入的是“你好”,在经过上面的过程之后,result为“你也好”相似的内容 		 *  由于终于回复给微信的也是xml格式的数据,全部须要将其封装为文本类型返回消息		 * */		result = new FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(), xmlEntity.getToUserName(), result);				return result;	}}

2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,能够避免非常多反复的推断,提高代码效率,代码例如以下:

package demo.entity;/** * 接收到的微信xml实体类 * @author pamchen-1 * */public class ReceiveXmlEntity {	private String ToUserName="";	private String FromUserName="";	private String CreateTime="";	private String MsgType="";	private String MsgId="";	private String Event="";	private String EventKey="";	private String Ticket="";	private String Latitude="";	private String Longitude="";	private String Precision="";	private String PicUrl="";	private String MediaId="";	private String Title="";	private String Description="";	private String Url="";	private String Location_X="";	private String Location_Y="";	private String Scale="";	private String Label="";	private String Content="";	private String Format="";	private String Recognition="";		public String getRecognition() {		return Recognition;	}	public void setRecognition(String recognition) {		Recognition = recognition;	}	public String getFormat() {		return Format;	}	public void setFormat(String format) {		Format = format;	}	public String getContent() {		return Content;	}	public void setContent(String content) {		Content = content;	}	public String getLocation_X() {		return Location_X;	}	public void setLocation_X(String locationX) {		Location_X = locationX;	}	public String getLocation_Y() {		return Location_Y;	}	public void setLocation_Y(String locationY) {		Location_Y = locationY;	}	public String getScale() {		return Scale;	}	public void setScale(String scale) {		Scale = scale;	}	public String getLabel() {		return Label;	}	public void setLabel(String label) {		Label = label;	}	public String getTitle() {		return Title;	}	public void setTitle(String title) {		Title = title;	}	public String getDescription() {		return Description;	}	public void setDescription(String description) {		Description = description;	}	public String getUrl() {		return Url;	}	public void setUrl(String url) {		Url = url;	}	public String getPicUrl() {		return PicUrl;	}	public void setPicUrl(String picUrl) {		PicUrl = picUrl;	}	public String getMediaId() {		return MediaId;	}	public void setMediaId(String mediaId) {		MediaId = mediaId;	}	public String getEventKey() {		return EventKey;	}	public void setEventKey(String eventKey) {		EventKey = eventKey;	}	public String getTicket() {		return Ticket;	}	public void setTicket(String ticket) {		Ticket = ticket;	}	public String getLatitude() {		return Latitude;	}	public void setLatitude(String latitude) {		Latitude = latitude;	}	public String getLongitude() {		return Longitude;	}	public void setLongitude(String longitude) {		Longitude = longitude;	}	public String getPrecision() {		return Precision;	}	public void setPrecision(String precision) {		Precision = precision;	}	public String getEvent() {		return Event;	}	public void setEvent(String event) {		Event = event;	}	public String getMsgId() {		return MsgId;	}	public void setMsgId(String msgId) {		MsgId = msgId;	}	public String getToUserName() {		return ToUserName;	}	public void setToUserName(String toUserName) {		ToUserName = toUserName;	}	public String getFromUserName() {		return FromUserName;	}	public void setFromUserName(String fromUserName) {		FromUserName = fromUserName;	}	public String getCreateTime() {		return CreateTime;	}	public void setCreateTime(String createTime) {		CreateTime = createTime;	}	public String getMsgType() {		return MsgType;	}	public void setMsgType(String msgType) {		MsgType = msgType;	}}
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;/** * 解析接收到的微信xml,返回消息对象 * @author pamchen-1 * */public class ReceiveXmlProcess {	/**	 * 解析微信xml消息	 * @param strXml	 * @return	 */	public ReceiveXmlEntity getMsgEntity(String strXml){		ReceiveXmlEntity msg = null;		try {			if (strXml.length() <= 0 || strXml == null)				return null;			 			// 将字符串转化为XML文档对象			Document document = DocumentHelper.parseText(strXml);			// 获得文档的根节点			Element root = document.getRootElement();			// 遍历根节点下全部子节点			Iterator
iter = root.elementIterator(); // 遍历全部结点 msg = new ReceiveXmlEntity(); //利用反射机制,调用set方法 //获取该实体的元类型 Class
c = Class.forName("demo.entity.ReceiveXmlEntity"); msg = (ReceiveXmlEntity)c.newInstance();//创建这个实体的对象 while(iter.hasNext()){ Element ele = (Element)iter.next(); //获取set方法中的參数字段(实体类的属性) Field field = c.getDeclaredField(ele.getName()); //获取set方法,field.getType())获取它的參数数据类型 Method method = c.getDeclaredMethod("set"+ele.getName(), field.getType()); //调用set方法 method.invoke(msg, ele.getText()); } } catch (Exception e) { // TODO: handle exception 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;/** * 调用图灵机器人api接口,获取智能回复内容 * @author pamchen-1 * */public class TulingApiProcess {	/**	 * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果	 * @param content	 * @return	 */	public String getTulingResult(String content){		/** 此处为图灵api接口,參数key须要自己去注冊申请,先以11111111取代 */		String apiUrl = "http://www.tuling123.com/openapi/api?key=11111111&info=";		String param = "";		try {			param = apiUrl+URLEncoder.encode(content,"utf-8");		} catch (UnsupportedEncodingException e1) {			// TODO Auto-generated catch block			e1.printStackTrace();		} //将參数转为url编码				/** 发送httpget请求 */		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);			//以code=100000为例,參考图灵机器人api文档			if(100000==json.getInt("code")){				result = json.getString("text");			}		} catch (JSONException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return result;	}}

2.4 将结果封装为微信规定的xml格式,并返回给1.1中创建的servlet接口。

package demo.process;import java.util.Date;/** * 封装终于的xml格式结果 * @author pamchen-1 * */public class FormatXmlProcess {	/**	 * 封装文字类的返回消息	 * @param to	 * @param from	 * @param content	 * @return	 */	public String formatXmlAnswer(String to, String from, String content) {		StringBuffer sb = new StringBuffer();		Date date = new Date();		sb.append("
"); sb.append(date.getTime()); sb.append("
0
"); return sb.toString(); }}

总结,以上便是微信公众平台开发的所有流程,总体来看并不复杂,要很感谢图灵机器人提供的api接口,帮我们攻克了智能回复这一高难度问题。其它类型的消息处理与演示样例中类似,有兴趣的开发人员能够联系我进行交流学习,希望本文对大家有所帮助。

本问中的代码演示样例已经上传到了csdn的个人资源中,有须要的能够去下载:http://download.csdn.net/detail/pamchen/7793979

转载地址:http://jdghl.baihongyu.com/

你可能感兴趣的文章
虚拟机Centos6.8安装MYSQL5.7,本地Navicat连接
查看>>
简单聊聊DOM
查看>>
【JavaScript】JavaScript Array 对象(数组)
查看>>
github 上有趣又实用的前端项目(持续更新,欢迎补充)
查看>>
opencv python 直方图均衡化
查看>>
HotFrameLearning 热门框架学习(前言)
查看>>
git团队开发流程
查看>>
【Under-the-hood-ReactJS-Part6】React源码解读
查看>>
深入理解css之vertical-align
查看>>
Laravel事件
查看>>
matlab绘制peano(皮亚诺)曲线和koch(科赫曲线,雪花曲线)分形曲线
查看>>
使用pipenv代替virtualenv管理python包
查看>>
Docker零基础入门指南(四):Docker容器使用
查看>>
React 深入系列4:组件的生命周期
查看>>
Mybatis之设计模式之迭代器模式
查看>>
房间号生成器
查看>>
CentOS 6.8 安装vsftpd
查看>>
js设计模式 --- 装饰设计模式
查看>>
Flask源代码阅读笔记(一)——应用启动
查看>>
IOS精品源码,仿探探UIButton封装iOS提示弹框迅速引导页自定义导航栏
查看>>