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

上传人:b****8 文档编号:30484770 上传时间:2023-08-15 格式:DOCX 页数:15 大小:19.03KB
下载 相关 举报
微信公众平台的Java的开发详解工程代码解析.docx_第1页
第1页 / 共15页
微信公众平台的Java的开发详解工程代码解析.docx_第2页
第2页 / 共15页
微信公众平台的Java的开发详解工程代码解析.docx_第3页
第3页 / 共15页
微信公众平台的Java的开发详解工程代码解析.docx_第4页
第4页 / 共15页
微信公众平台的Java的开发详解工程代码解析.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

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

《微信公众平台的Java的开发详解工程代码解析.docx》由会员分享,可在线阅读,更多相关《微信公众平台的Java的开发详解工程代码解析.docx(15页珍藏版)》请在冰豆网上搜索。

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

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

说明:

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

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

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

在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用来实现,相关解释已经在注释中说明,代码如下:

[java]

1.package;

2.

3.import

4.import

5.import

6.import

7.import

8.

9.import

10.import

11.import

12.import

13.

14.import

15./**

16.*微信服务端收发消息接口

17.*

18.*@authorpamchen-1

19.*

20.*/

21.publicclassWechatServletextendsHttpServlet{

22.

23./**

24.*ThedoGetmethodoftheservlet.

25.*

26.*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

27.*

28.*@paramrequest

29.*therequestsendbytheclienttotheserver

30.*@paramresponse

31.*theresponsesendbytheservertotheclient

32.*@throwsServletException

33.*ifanerroroccurred

34.*@throwsIOException

35.*ifanerroroccurred

36.*/

37.publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

38.throwsServletException,IOException{

39.("UTF-8");

40.("UTF-8");

41.

42./**读取接收到的xml消息*/

43.StringBuffersb=newStringBuffer();

44.InputStreamis=();

45.InputStreamReaderisr=newInputStreamReader(is,"UTF-8");

46.BufferedReaderbr=newBufferedReader(isr);

47.Strings="";

48.while((s=())!

=null){

49.(s);

50.}

51.Stringxml=();rocessWechatMag(xml);

52.}

53.

54.try{

55.OutputStreamos=();

56.("UTF-8"));

57.();

58.();

59.}catch(Exceptione){

60.();

61.}

62.}

63.

64./**

65.*ThedoPostmethodoftheservlet.

66.*

67.*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto

68.*post.

69.*

70.*@paramrequest

71.*therequestsendbytheclienttotheserver

72.*@paramresponse

73.*theresponsesendbytheservertotheclient

74.*@throwsServletException

75.*ifanerroroccurred

76.*@throwsIOException

77.*ifanerroroccurred

78.*/

79.publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

80.throwsServletException,IOException{

81.doGet(request,response);

82.}

83.

84.}

相应的配置信息如下,在生成的同时,可自动生成中的配置。

前面所提到的url处可以填写例如:

1.http;etMsgEntity(xml);

2.

3./**以文本消息为例,调用图灵机器人api接口,获取回复内容*/

4.Stringresult="";

5.if("text".endsWith())){

6.result=newTulingApiProcess().getTulingResult());

7.}

8.

9./**此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容

10.*因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息

11.**/

12.result=newFormatXmlProcess().formatXmlAnswer(),(),result);

13.

14.returnresult;

15.}

16.}

解析接收到的xml数据,此处有两个类,和,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:

[java]

1.package;

2./**

3.*接收到的实体类

4.*@authorpamchen-1

5.*

6.*/

7.publicclassReceiveXmlEntity{

8.privateStringToUserName="";

9.privateStringFromUserName="";

10.privateStringCreateTime="";

11.privateStringMsgType="";

12.privateStringMsgId="";

13.privateStringEvent="";

14.privateStringEventKey="";

15.privateStringTicket="";

16.privateStringLatitude="";

17.privateStringLongitude="";

18.privateStringPrecision="";

19.privateStringPicUrl="";

20.privateStringMediaId="";

21.privateStringTitle="";

22.privateStringDescription="";

23.privateStringUrl="";

24.privateStringLocation_X="";

25.privateStringLocation_Y="";

26.privateStringScale="";

27.privateStringLabel="";

28.privateStringContent="";

29.privateStringFormat="";

30.privateStringRecognition="";

31.

32.publicStringgetRecognition(){

33.returnRecognition;

34.}

35.publicvoidsetRecognition(Stringrecognition){

36.Recognition=recognition;

37.}

38.publicStringgetFormat(){

39.returnFormat;

40.}

41.publicvoidsetFormat(Stringformat){

42.Format=format;

43.}

44.publicStringgetContent(){

45.returnContent;

46.}

47.publicvoidsetContent(Stringcontent){

48.Content=content;

49.}

50.publicStringgetLocation_X(){

51.returnLocation_X;

52.}

53.publicvoidsetLocation_X(StringlocationX){

54.Location_X=locationX;

55.}

56.publicStringgetLocation_Y(){

57.returnLocation_Y;

58.}

59.publicvoidsetLocation_Y(StringlocationY){

60.Location_Y=locationY;

61.}

62.publicStringgetScale(){

63.returnScale;

64.}

65.publicvoidsetScale(Stringscale){

66.Scale=scale;

67.}

68.publicStringgetLabel(){

69.returnLabel;

70.}

71.publicvoidsetLabel(Stringlabel){

72.Label=label;

73.}

74.publicStringgetTitle(){

75.returnTitle;

76.}

77.publicvoidsetTitle(Stringtitle){

78.Title=title;

79.}

80.publicStringgetDescription(){

81.returnDescription;

82.}

83.publicvoidsetDescription(Stringdescription){

84.Description=description;

85.}

86.publicStringgetUrl(){

87.returnUrl;

88.}

89.publicvoidsetUrl(Stringurl){

90.Url=url;

91.}

92.publicStringgetPicUrl(){

93.returnPicUrl;

94.}

95.publicvoidsetPicUrl(StringpicUrl){

96.PicUrl=picUrl;

97.}

98.publicStringgetMediaId(){

99.returnMediaId;

100.}

101.publicvoidsetMediaId(StringmediaId){

102.MediaId=mediaId;

103.}

104.publicStringgetEventKey(){

105.returnEventKey;

106.}

107.publicvoidsetEventKey(StringeventKey){

108.EventKey=eventKey;

109.}

110.publicStringgetTicket(){

111.returnTicket;

112.}

113.publicvoidsetTicket(Stringticket){

114.Ticket=ticket;

115.}

116.publicStringgetLatitude(){

117.returnLatitude;

118.}

119.publicvoidsetLatitude(Stringlatitude){

120.Latitude=latitude;

121.}

122.publicStringgetLongitude(){

123.returnLongitude;

124.}

125.publicvoidsetLongitude(Stringlongitude){

126.Longitude=longitude;

127.}

128.publicStringgetPrecision(){

129.returnPrecision;

130.}

131.publicvoidsetPrecision(Stringprecision){

132.Precision=precision;

133.}

134.publicStringgetEvent(){

135.returnEvent;

136.}

137.publicvoidsetEvent(Stringevent){

138.Event=event;

139.}

140.publicStringgetMsgId(){

141.returnMsgId;

142.}

143.publicvoidsetMsgId(StringmsgId){

144.MsgId=msgId;

145.}

146.publicStringgetToUserName(){

147.returnToUserName;

148.}

149.publicvoidsetToUserName(StringtoUserName){

150.ToUserName=toUserName;

151.}

152.publicStringgetFromUserName(){

153.returnFromUserName;

154.}

155.publicvoidsetFromUserName(StringfromUserName){

156.FromUserName=fromUserName;

157.}

158.publicStringgetCreateTime(){

159.returnCreateTime;

160.}

161.publicvoidsetCreateTime(StringcreateTime){

162.CreateTime=createTime;

163.}

164.publicStringgetMsgType(){

165.returnMsgType;

166.}

167.publicvoidsetMsgType(StringmsgType){

168.MsgType=msgType;

169.}

170.}

[java]

1.package;

2.

3.import

4.import

5.import

6.import

7.import

8.import

9.

10.import

11./**

12.*解析接收到的,返回消息对象

13.*@authorpamchen-1

14.*

15.*/

16.publicclassReceiveXmlProcess{

17./**

18.*解析消息

19.*@paramstrXml

20.*@return

21.*/

22.publicReceiveXmlEntitygetMsgEntity(StringstrXml){

23.ReceiveXmlEntitymsg=null;

24.try{

25.if()<=0||strXml==null)

26.returnnull;

27.

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.

15.try{

16.JSONObjectjson=newJSONObject(result);

17.//以code=100000为例,参考图灵机器人api文档

18.if(100000==("code")){

19.result=("text");

20.}

21.}catch(JSONExceptione){

22.//TODOAuto-generatedcatchblock

23.();

24.}

25.returnresult;

26.}

27.}

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

[java]

1.package;

2.

3.import

4./**

5.*封装最终的xml格式结果

6.*@authorpamchen-1

7.*

8.*/

9.publicclassFormatXmlProcess{

10./**

11.*封装文字类的返回消息

12.*@paramto

13.*@paramfrom

14.*@paramcontent

15.*@return

16.*/

17.publicStringformatXmlAnswer(Stringto,Stringfrom,Stringcontent){

18.StringBuffersb=newStringBuffer();

19.Datedate=newDate();

20.("

[CDATA[");

21.(to);

22.("]]>

[CDATA[");

23.(from);

24.("]]>");

25.());

26.("

[CDATA[text]]>

[CDATA[");

27.(content);

28.("]]>0");

29.return();

30.}

31.}

总结,以上便是微信公众平台开发的全部流程,整体来看并不复杂,要非常感谢图灵机器人提供的api接口,帮我们解决了智能回复这一高难度问题。

其他类型的消息处理与示例中类似,有兴趣的开发者可以联系我进行交流学习,希望本文对大家有所帮助。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 唐诗宋词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1