公众平台的Java开发详解(工程代码+解析)
说明:
本次的教程主要是对公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学开发的人一头雾水,所以总结自己的开发经验,将开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
本次的教程主要是对公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学开发的人一头雾水,所以总结自己的开发经验,将开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
[java] view plaincopy
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. <br>
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 =&InputStream();
45. InputStreamReader isr = new InputStreamReader(is, "UTF-8");
46. BufferedReader br = new BufferedReader(isr);
47. String s = "";
48. while ((s =&adLine()) != null) {
49. sb.append(s);
50. }
51. String xml =&String(); //次即为接收到端发送过来的xml数据
52.
53. String result = "";
54. /** 判断是否是接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */
55. String echostr =&Parameter("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 =&OutputStream();
65. os.Bytes("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. <br>
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 相应的l配置信息如下,在生成WechatServlet.java的同时,可自动生成l中的配置。前面所提到的url处可以填写例如:
http;//服务器地址/项目名/wechat.do
[html] view plaincopy
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.5"
3. xmlns="java.sun/xml/ns/javaee"
4. xmlns:xsi="/2001/XMLSchema-instance"
5. xsi:schemaLocation="java.sun/xml/ns/javaee
6. java.sun/xml/ns/javaee/web-app_2_5.xsd">
7. 代码转换<servlet>
8. <description>This is the description of my J2EE component</description>
9. <display-name>This is the display name of my J2EE component</display-name>
10. <servlet-name>WechatServlet</servlet-name>
11. <servlet-class>demo.servlet.WechatServlet</servlet-class>
12. </servlet>
13.
14. <servlet-mapping>
15. <servlet-name>WechatServlet</servlet-name>
16. <url-pattern>/wechat.do</url-pattern>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论