当前位置:文档之家› 微信公众平台的Java的开发详解工程代码解析

微信公众平台的Java的开发详解工程代码解析

说明:本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。

在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。

另外,在做内容回复时用到了,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。

在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用来实现,相关解释已经在注释中说明,代码如下:[java]1.package;2.import3.import4.import5.import6.import7.import8.import9.import10.import11.import12./**13.*微信服务端收发消息接口14.*15.*@authorpamchen-116.*17.*/18.publicclass WechatServlet extends HttpServlet{19./**20.*ThedoGetmethodoftheservlet.<br>21.*22.*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.23.*24.*@paramrequest25.*therequestsendbytheclienttotheserver26.*@paramresponse27.*theresponsesendbytheservertotheclient28.*@throwsServletException29.*ifanerroroccurred30.*@throwsIOException31.*ifanerroroccurred32.*/33.publicvoid doGet(HttpServletRequestrequest,HttpServletResponseresponse)34.throws ServletException,IOException{35.("UTF-8");36.("UTF-8");37./**读取接收到的xml消息*/38.StringBuffersb=new StringBuffer();39.InputStreamis=();40.InputStreamReaderisr=new InputStreamReader(is,"UTF-8");41.BufferedReaderbr=new BufferedReader(isr);42.Strings="";43.while((s=())!=null){44.(s);45.}46.Stringxml=();rocessWechatMag(xml);47.}48.try{49.OutputStreamos=();50.("UTF-8"));51.();52.();53.}catch(Exceptione){54.();55.}56.}57./**58.*ThedoPostmethodoftheservlet.<br>59.*60.*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto61.*post.62.*63.*@paramrequest64.*therequestsendbytheclienttotheserver65.*@paramresponse66.*theresponsesendbytheservertotheclient67.*@throwsServletException68.*ifanerroroccurred69.*@throwsIOException70.*ifanerroroccurred71.*/72.publicvoid doPost(HttpServletRequestrequest,HttpServletResponseresponse)73.throws ServletException,IOException{74.doGet(request,response);75.}76.}相应的配置信息如下,在生成的同时,可自动生成中的配置。

前面所提到的url处可以填写例如:1.http;etMsgEntity(xml);2./**以文本消息为例,调用图灵机器人api接口,获取回复内容*/3.Stringresult="";4.if("text".endsWith())){5.result=new TulingApiProcess().getTulingResult());6.}7./**此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容8.*因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息9.**/10.result=new FormatXmlProcess().formatXmlAnswer(),(),result);11.return result;12.}13.}解析接收到的xml数据,此处有两个类,和,通过反射的机制动态调用实体类中的set 方法,可以避免很多重复的判断,提高代码效率,代码如下:[java]1.package;2./**3.*接收到的实体类4.*@authorpamchen-15.*6.*/7.publicclass ReceiveXmlEntity{8.private StringToUserName="";9.private StringFromUserName="";10.private StringCreateTime="";11.private StringMsgType="";12.private StringMsgId="";13.private StringEvent="";14.private StringEventKey="";15.private StringTicket="";16.private StringLatitude="";17.private StringLongitude="";18.private StringPrecision="";19.private StringPicUrl="";20.private StringMediaId="";21.private StringTitle="";22.private StringDescription="";23.private StringUrl="";24.private StringLocation_X="";25.private StringLocation_Y="";26.private StringScale="";27.private StringLabel="";28.private StringContent="";29.private StringFormat="";30.private StringRecognition="";31.public StringgetRecognition(){32.return Recognition;33.}34.publicvoid setRecognition(Stringrecognition){35.Recognition=recognition;36.}37.public StringgetFormat(){38.return Format;39.}40.publicvoid setFormat(Stringformat){41.Format=format;42.}43.public StringgetContent(){44.return Content;45.}46.publicvoid setContent(Stringcontent){47.Content=content;48.}49.public StringgetLocation_X(){50.return Location_X;51.}52.publicvoid setLocation_X(StringlocationX){53.Location_X=locationX;54.}55.public StringgetLocation_Y(){56.return Location_Y;57.}58.publicvoid setLocation_Y(StringlocationY){59.Location_Y=locationY;60.}61.public StringgetScale(){62.return Scale;63.}64.publicvoid setScale(Stringscale){65.Scale=scale;66.}67.public StringgetLabel(){68.return Label;69.}70.publicvoid setLabel(Stringlabel){bel=label;72.}73.public StringgetTitle(){74.return Title;75.}76.publicvoid setTitle(Stringtitle){77.Title=title;78.}79.public StringgetDescription(){80.return Description;81.}82.publicvoid setDescription(Stringdescription){83.Description=description;84.}85.public StringgetUrl(){86.return Url;87.}88.publicvoid setUrl(Stringurl){89.Url=url;90.}91.public StringgetPicUrl(){92.return PicUrl;93.}94.publicvoid setPicUrl(StringpicUrl){95.PicUrl=picUrl;96.}97.public StringgetMediaId(){98.return MediaId;99.}100.publicvoid setMediaId(StringmediaId){ 101.MediaId=mediaId;102.}103.public StringgetEventKey(){104.return EventKey;105.}106.publicvoid setEventKey(StringeventKey){ 107.EventKey=eventKey;108.}109.public StringgetTicket(){110.return Ticket;111.}112.publicvoid setTicket(Stringticket){113.Ticket=ticket;114.}115.public StringgetLatitude(){116.return Latitude;117.}118.publicvoid setLatitude(Stringlatitude){ titude=latitude;120.}121.public StringgetLongitude(){122.return Longitude;123.}124.publicvoid setLongitude(Stringlongitude){ 125.Longitude=longitude;126.}127.public StringgetPrecision(){128.return Precision;129.}130.publicvoid setPrecision(Stringprecision){ 131.Precision=precision;132.}133.public StringgetEvent(){134.return Event;135.}136.publicvoid setEvent(Stringevent){137.Event=event;138.}139.public StringgetMsgId(){140.return MsgId;141.}142.publicvoid setMsgId(StringmsgId){143.MsgId=msgId;144.}145.public StringgetToUserName(){146.return ToUserName;147.}148.publicvoid setToUserName(StringtoUserName){149.ToUserName=toUserName;150.}151.public StringgetFromUserName(){152.return FromUserName;153.}154.publicvoid setFromUserName(StringfromUserName){ 155.FromUserName=fromUserName;156.}157.public StringgetCreateTime(){158.return CreateTime;159.}160.publicvoid setCreateTime(StringcreateTime){161.CreateTime=createTime;162.}163.public StringgetMsgType(){164.return MsgType;165.}166.publicvoid setMsgType(StringmsgType){167.MsgType=msgType;168.}169.}[java]1.package;2.import3.import4.import5.import6.import7.import8.import9./**10.*解析接收到的,返回消息对象11.*@authorpamchen-112.*13.*/14.publicclass ReceiveXmlProcess{16.*解析消息17.*@paramstrXml18.*@return19.*/20.public ReceiveXmlEntitygetMsgEntity(StringstrXml){21.ReceiveXmlEntitymsg=null;22.try{23.if()<=0||strXml==null)24.returnnull;1.xecute(request);2.if().getStatusCode()==200){3.result=());4.}5.}catch(ClientProtocolExceptione){6.();7.}catch(IOExceptione){8.();9.}10./**请求失败处理*/11.if(null==result){12.return"对不起,你说的话真是太高深了……";13.}14.try{15.JSONObjectjson=new JSONObject(result);16.//以code=100000为例,参考图灵机器人api文档17.if(100000==("code")){18.result=("text");19.}20.}catch(JSONExceptione){21.//TODOAuto-generatedcatchblock22.();23.}24.return result;25.}26.}将结果封装为微信规定的xml格式,并返回给中创建的servlet接口。

相关主题