微信公众平台的Java的开发详解(工程代码+解析).pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《微信公众平台的Java的开发详解(工程代码+解析).pdf》由会员分享,可在线阅读,更多相关《微信公众平台的Java的开发详解(工程代码+解析).pdf(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、微信公众平台的Java开发详解(工程代码+解析)说明:本次的教程主要是对 微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是 xml 格式的数据。另外,在做内容回复时用到了 图灵机器人的 api 接口,这是一个 自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写
2、url 和token,所谓 url 就是我们自己服务器的接口,用 WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:javaview plaincopy1.package demo.servlet;2.3.import java.io.BufferedReader;4.import java.io.IOException;5.import java.io.InputStream;6.import java.io.InputStreamReader;7.import java.io.OutputStream;8.9.import javax.servlet.Serv
3、letException;10.import javax.servlet.http.HttpServlet;11.import javax.servlet.http.HttpServletRequest;12.import javax.servlet.http.HttpServletResponse;13.14.import demo.process.WechatProcess;15./*16.*微信服务端收发消息接口17.*18.*author pamchen-119.*20.*/21.publicclass WechatServlet extends HttpServlet 22.23./
4、*24.*The doGet method of the servlet.25.*26.*This method is called when a form has its tag value method equals to get.27.*28.*param request29.*the request send by the client to the server30.*param response31.*the response send by the server to the client32.*throws ServletException33.*if an error occ
5、urred34.*throws IOException35.*if an error occurred36.*/37.publicvoid doGet(HttpServletRequest request,HttpServletResponse response)38.throws ServletException,IOException 39.request.setCharacterEncoding(UTF-8);40.response.setCharacterEncoding(UTF-8);41.42./*读取接收到的xml 消息 */43.StringBuffer sb=new Stri
6、ngBuffer();44.InputStream is=request.getInputStream();45.InputStreamReader isr=new InputStreamReader(is,UTF-8);46.BufferedReader br=new BufferedReader(isr);47.String s=;48.while (s=br.readLine()!=null)49.sb.append(s);50.51.String xml=sb.toString();/次即为接收到微信端发送过来的xml 数据52.53.String result=;54./*判断是否是
7、微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回*/55.String echostr=request.getParameter(echostr);56.if (echostr!=null&echostr.length()1)57.result=echostr;58.else 59./正常的微信处理流程60.result=new WechatProcess().processWechatMag(xml);61.62.63.try 64.OutputStream os=response.getOutputStream();65.os.write(result.
8、getBytes(UTF-8);66.os.flush();67.os.close();68.catch (Exception e)69.e.printStackTrace();70.71.72.73./*74.*The doPost method of the servlet.75.*76.*This method is called when a form has its tag value method equals to77.*post.78.*79.*param request80.*the request send by the client to the server81.*pa
9、ram response82.*the response send by the server to the client83.*throws ServletException84.*if an error occurred85.*throws IOException86.*if an error occurred87.*/88.publicvoid doPost(HttpServletRequest request,HttpServletResponse response)89.throws ServletException,IOException 90.doGet(request,resp
10、onse);91.92.93.1.2 相应的 web.xml配置信息如下,在生成 WechatServlet.java的同时,可自动生成 web.xml中的配置。前面所提到的url 处可以填写例如:http;/服务器地址/项目名/wechat.dohtmlview plaincopy1.2.7.8.This is the description of my J2EE component9.This is the display name of my J2EE component10.WechatServlet11.demo.servlet.WechatServlet12.13.14.15.We
11、chatServlet16./wechat.do17.18.19.index.jsp20.21.1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。下面就讲解其核心部分解析接收到的xml 数据,并以文本类消息为例,通过图灵机器人 api 接口实现智能回复。2.1 首先看一下整体流程处理代码,包括:xml 数据处理、调用图灵api、封装返回的 xml 数据。javaview plaincopy1.package demo.process;2.3.import java.util.Date;4.5.import demo.entit
12、y.ReceiveXmlEntity;6.7./*8.*微信 xml 消息处理流程逻辑类9.*author pamchen-110.*11.*/12.publicclass WechatProcess 13./*14.*解析处理 xml、获取智能回复结果(通过图灵机器人api接口)15.*param xml 接收到的微信数据16.*return 最终的解析结果(xml 格式数据)17.*/18.public String processWechatMag(String xml)19./*解析 xml 数据 */20.ReceiveXmlEntity xmlEntity=new ReceiveX
13、mlProcess().getMsgEntity(xml);21.22./*以文本消息为例,调用图灵机器人api 接口,获取回复内容*/23.String result=;24.if(text.endsWith(xmlEntity.getMsgType()25.result=new TulingApiProcess().getTulingResult(xmlEntity.getContent();26.27.28./*此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容29.*因为最终回复给微信的也是xml 格式的数据,所有需要将其封装为文本类型返回消息30
14、.*/31.result=new FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(),xmlEntity.getToUserName(),result);32.33.return result;34.35.2.2 解析接收到的 xml 数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set 方法,可以避免很多重复的判断,提高代码效率,代码如下:javaview plaincopy1.package demo.entity;2./
15、*3.*接收到的微信xml 实体类4.*author pamchen-15.*6.*/7.publicclass ReceiveXmlEntity 8.private String ToUserName=;9.private String FromUserName=;10.private String CreateTime=;11.private String MsgType=;12.private String MsgId=;13.private String Event=;14.private String EventKey=;15.private String Ticket=;16.pr
16、ivate String Latitude=;17.private String Longitude=;18.private String Precision=;19.private String PicUrl=;20.private String MediaId=;21.private String Title=;22.private String Description=;23.private String Url=;24.private String Location_X=;25.private String Location_Y=;26.private String Scale=;27
17、.private String Label=;28.private String Content=;29.private String Format=;30.private String Recognition=;31.32.public String getRecognition()33.return Recognition;34.35.publicvoid setRecognition(String recognition)36.Recognition=recognition;37.38.public String getFormat()39.return Format;40.41.pub
18、licvoid setFormat(String format)42.Format=format;43.44.public String getContent()45.return Content;46.47.publicvoid setContent(String content)48.Content=content;49.50.public String getLocation_X()51.return Location_X;52.53.publicvoid setLocation_X(String locationX)54.Location_X=locationX;55.56.publi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 公众 平台 Java 开发 详解 工程 代码 解析
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内