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

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

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

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

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

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

说明:

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

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

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

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

[java] viewplaincopy

1.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.ServletException;  

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-1 

19. *  

20. */  

21.public class WechatServlet extends HttpServlet {  

22.  

23.    /** 

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 request 

29.     *            the request send by the client to the server 

30.     * @param response 

31.     *            the response send by the server to the client 

32.     * @throws ServletException 

33.     *             if an error occurred 

34.     * @throws IOException 

35.     *             if an error occurred 

36.     */  

37.    public void 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 StringBuffer();  

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.        /** 判断是否是微信接入激活验证,只有首次接入验证时才会收到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.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 to 

77.     * post. 

78.     *  

79.     * @param request 

80.     *            the request send by the client to the server 

81.     * @param response 

82.     *            the response send by the server to the client 

83.     * @throws ServletException 

84.     *             if an error occurred 

85.     * @throws IOException 

86.     *             if an error occurred 

87.     */  

88.    public void doPost(HttpServletRequest request, HttpServletResponse response)  

89.            throws ServletException, IOException {  

90.        doGet(request, response);  

91.    }  

92.  

93.}  

1.2相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。

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

http;//服务器地址/项目名/wechat.do

[html] viewplaincopy

1.

xml version="1.0" encoding="UTF-8"?

>  

2.

3.    xmlns="   

4.    xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"   

5.    xsi:

schemaLocation="   

6.      

7.    

8.    This is the description of my J2EE component  

9.    This is the display name of my J2EE component  

10.    WechatServlet  

11.    demo.servlet.WechatServlet  

12.    

13.  

14.    

15.    WechatServlet  

16.    /wechat.do  

17.    

18.    

19.    index.jsp  

20.    

21.  

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

下面就

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

当前位置:首页 > IT计算机 > 互联网

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

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