1、微信公众平台的Java的开发详解工程代码+解析15P说明:本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和t
2、oken,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:javaview plaincopy1. packagedemo.servlet;2. 3. importjava.io.BufferedReader;4. importjava.io.IOException;5. importjava.io.InputStream;6. importjava.io.InputStreamReader;7. importjava.io.OutputStream;8. 9. importjavax.servlet.ServletExce
3、ption;10. importjavax.servlet.http.HttpServlet;11. importjavax.servlet.http.HttpServletRequest;12. importjavax.servlet.http.HttpServletResponse;13. 14. importdemo.process.WechatProcess;15. /*16. *微信服务端收发消息接口17. *18. *authorpamchen-119. *20. */21. publicclassWechatServletextendsHttpServlet22. 23. /*2
4、4. *ThedoGetmethodoftheservlet.25. *26. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.27. *28. *paramrequest29. *therequestsendbytheclienttotheserver30. *paramresponse31. *theresponsesendbytheservertotheclient32. *throwsServletException33. *ifanerroroccurred34. *throwsIOException35. *i
5、fanerroroccurred36. */37. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)38. throwsServletException,IOException39. request.setCharacterEncoding(UTF-8);40. response.setCharacterEncoding(UTF-8);41. 42. /*读取接收到的xml消息*/43. StringBuffersb=newStringBuffer();44. InputStreamis=request
6、.getInputStream();45. InputStreamReaderisr=newInputStreamReader(is,UTF-8);46. BufferedReaderbr=newBufferedReader(isr);47. Strings=;48. while(s=br.readLine()!=null)49. sb.append(s);50. 51. Stringxml=sb.toString();/次即为接收到微信端发送过来的xml数据52. 53. Stringresult=;54. /*判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需
7、要把它直接返回*/55. Stringechostr=request.getParameter(echostr);56. if(echostr!=null&echostr.length()1)57. result=echostr;58. else59. /正常的微信处理流程60. result=newWechatProcess().processWechatMag(xml);61. 62. 63. try64. OutputStreamos=response.getOutputStream();65. os.write(result.getBytes(UTF-8);66. os.flush()
8、;67. os.close();68. catch(Exceptione)69. e.printStackTrace();70. 71. 72. 73. /*74. *ThedoPostmethodoftheservlet.75. *76. *Thismethodiscalledwhenaformhasitstagvaluemethodequalsto77. *post.78. *79. *paramrequest80. *therequestsendbytheclienttotheserver81. *paramresponse82. *theresponsesendbytheservert
9、otheclient83. *throwsServletException84. *ifanerroroccurred85. *throwsIOException86. *ifanerroroccurred87. */88. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)89. throwsServletException,IOException90. doGet(request,response);91. 92. 93. 1.2 相应的web.xml配置信息如下,在生成WechatServlet.
10、java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;/服务器地址/项目名/wechat.dohtmlview plaincopy1. 2. web-appversion=2.53. xmlns=4. xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance5. xsi:schemaLocation=6. 7. 8. ThisisthedescriptionofmyJ2EEcomponent9. ThisisthedisplaynameofmyJ2EEcomponent10. WechatServlet11. demo.servlet.WechatServlet12. 13. 14. 15. WechatServlet16. /wechat.do17. 18. 19. index.jsp20. 21. 1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。下面就
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1