


A must-read introduction to the access guide for newbies to JAVA WeChat development
Mar 26, 2017 pm 01:54 PMI believe that many people are no longer unfamiliar with WeChat development. I have also been tortured by various problems since I was a rookie in WeChat development, and then I went to search engines to search for various articles to read, but they were basically scattered information, without a unified , systematically explains how to develop WeChat applications. The author combines his actual development experience to summarize and share it with beginners of WeChat development or developers who are studying.
This article mainly explains how to build a WeChat development platform. The ancients said: "Sharpening a knife is worth the effort." We also need to prepare some necessary conditions when developing applications.
Preparation
1. Download Jdk1.7 Installation
2. Download Tomcat 7.0
3. Download Myeclipse 6.5
4. Download Json-lib third-party jar .
1. Apply for a public IP address and publish your own application.
For example: 192.168.1.102
2. Open myeclipse and create a project
As shown below:
3. Create a WeChat application access Servlet class
JAVA implementation class: WeixinServlet
package?com.wx.servlet; import?java.io.IOException; import?java.io.InputStream; import?java.io.OutputStream; import?java.security.MessageDigest; import?java.security.NoSuchAlgorithmException; import?java.util.Arrays; import?java.util.logging.Logger; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?net.sf.json.JSONObject; import?net.sf.json.xml.XMLSerializer; ? /** ?*?@author?haibing.xiao ?*?@since?jdk1.6 ?*?@version?1.0 ?*/ public?class?WeixinServlet?extends?HttpServlet{ private?Logger?log?=Logger.getLogger(this.getClass().getName()); private?static?final?long?serialVersionUID?=?1L; private???String?Token; private???String?echostr; @Override protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response) throws?ServletException,?IOException?{ ?connect(request,response); } @Override protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response) throws?ServletException,?IOException?{ message(request,response); } ? /** ?*@author?haibing.xiao ?*@return?? ?*@exception? ?*@param ?*? ?*?<p>接入連接生效驗(yàn)證</p> ?*/ private?void?connect(HttpServletRequest?request,HttpServletResponse?response)?throws?ServletException,?IOException{ log.info("RemoteAddr:?"+?request.getRemoteAddr()); log.info("QueryString:?"+?request.getQueryString()); ?if(!accessing(request,?response)){ ?log.info("服務(wù)器接入失敗......."); ?return?; ?} String?echostr=getEchostr(); if(echostr!=null?&&?!"".equals(echostr)){ log.info("服務(wù)器接入生效.........."); response.getWriter().print(echostr);//完成相互認(rèn)證 } } /** ?*?@author?haibing.xiao ?*?Date?2013-05-29 ?*?@return?boolean ?*?@exception?ServletException,?IOException ?*?@param ?* ?*<p>用來接收微信公眾平臺(tái)的驗(yàn)證</p>? ?*/ private?boolean?accessing(HttpServletRequest?request,?HttpServletResponse?response)throws?ServletException,?IOException?{ String?signature?=?request.getParameter("signature"); String?timestamp?=?request.getParameter("timestamp"); String?nonce?=?request.getParameter("nonce"); String?echostr?=?request.getParameter("echostr"); if(?isEmpty(signature)){ return?false; } if(isEmpty(timestamp)){ return?false; } if(isEmpty(nonce)){ return?false; } if(isEmpty(echostr)){ return?false; } String[]?ArrTmp?=?{?Token,?timestamp,?nonce?}; Arrays.sort(ArrTmp); StringBuffer?sb?=?new?StringBuffer(); for?(int?i?=?0;?i?XML組裝組件 ?*/ ?private?void?message(HttpServletRequest?request,HttpServletResponse?response)?throws?ServletException,?IOException{ ? InputStream?is?=?request.getInputStream(); //?取HTTP請(qǐng)求流長(zhǎng)度 int?size?=?request.getContentLength(); //?用于緩存每次讀取的數(shù)據(jù) byte[]?buffer?=?new?byte[size]; //?用于存放結(jié)果的數(shù)組 byte[]?xmldataByte?=?new?byte[size]; int?count?=?0; int?rbyte?=?0; //?循環(huán)讀取 while?(count?業(yè)務(wù)轉(zhuǎn)發(fā)組件 ?*? ?*/ ??private?void??manageMessage(String?requestStr,HttpServletRequest?request,HttpServletResponse?response)throws?ServletException,IOException{ ???? ????String?responseStr; ??? try?{ ?XMLSerializer?xmlSerializer=new?XMLSerializer(); ?JSONObject?jsonObject?=(JSONObject)?xmlSerializer.read(requestStr); ?String?event?=jsonObject.getString("Event"); ?String?msgtype?=jsonObject.getString("MsgType"); ?if("CLICK".equals(event)?&&?"event".equals(msgtype)){?//菜單click事件 ?String?eventkey?=jsonObject.getString("EventKey"); ?if("hytd_001".equals(eventkey)){?//?hytd_001?這是好友團(tuán)隊(duì)按鈕的標(biāo)志值 ?jsonObject.put("Content",?"歡迎使用好友團(tuán)隊(duì)菜單click按鈕."); ?} ?} ?responseStr?=creatRevertText(jsonObject);//創(chuàng)建XML ?log.info("responseStr:"+responseStr); ?OutputStream?os?=response.getOutputStream(); ?os.write(responseStr.getBytes("UTF-8")); }???catch?(Exception?e)?{ e.printStackTrace(); } } private?String?creatRevertText(JSONObject?jsonObject){ ???? StringBuffer?revert?=new?StringBuffer(); ???? revert.append("<xml>"); ???? revert.append("<tousername></tousername>"); ???? revert.append("<fromusername></fromusername>"); ???? revert.append("<createtime>"+jsonObject.get("CreateTime")+"</createtime>"); ???? revert.append("<msgtype></msgtype>"); ???? revert.append("<content></content>"); ???? revert.append("<funcflag>0</funcflag>"); ???? revert.append("</xml>"); ???? return?revert.toString(); ????} @Override public?void?init()?throws?ServletException?{ Token="test123"; } ? private?boolean?isEmpty(String?str){ return?null?==str?||?"".equals(str)???true?:false; } private?String?trim(String?str){ return?null?!=str????str.trim()?:?str; } }
4. Locally deployed application visit http://localhost:port No./Context/wenxin
5 .Apply to become a developer
Visit http://mp.weixin.qq.com , turn on developer mode. Fill in the URL and take, and the application is successful, as shown below:
The above is the detailed content of A must-read introduction to the access guide for newbies to JAVA WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)