相信很多人對微信開發已經不那麼陌生,我也是從一個微信開發的菜鳥經過各種問題的折磨,然後去搜尋引擎搜尋各種文章閱讀,但是基本上都是零散的資料,沒有一個統一、系統性的闡述微信應用如何開發。作者結合自己的實際開發經驗,歸納整理出來分享給微信開發的入門者或正在研究的開發者。
這篇文章主要闡述如何建構一個微信開發平台。古人雲:「磨刀不負砍才工」。我們開發應用也需要準備一些必要的條件。
準備工作
1. 下載Jdk1.7 安裝
2. 下載Tomcat 7.0
3. 下載Myeclipse 6.5
4. 下載Json-lib第三方jar .
1. 申請公網IP位址,發佈自己的應用程式。
例如: 192.168.1.102
2. 開啟myeclipse 建立一個工程
#如下圖所示:
## 3. 建立一個微信應用程式存取Servlet類別
JAVA實作類別: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>接入连接生效验证</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("服务器接入失败......."); return ; } String echostr=getEchostr(); if(echostr!=null && !"".equals(echostr)){ log.info("服务器接入生效.........."); response.getWriter().print(echostr);//完成相互认证 } } /** * @author haibing.xiao * Date 2013-05-29 * @return boolean * @exception ServletException, IOException * @param * *<p>用来接收微信公众平台的验证</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请求流长度 int size = request.getContentLength(); // 用于缓存每次读取的数据 byte[] buffer = new byte[size]; // 用于存放结果的数组 byte[] xmldataByte = new byte[size]; int count = 0; int rbyte = 0; // 循环读取 while (count 业务转发组件 * */ 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 这是好友团队按钮的标志值 jsonObject.put("Content", "欢迎使用好友团队菜单click按钮."); } } responseStr =creatRevertText(jsonObject);//创建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. 本地部署應用 存取http://localhost:連接埠存取埠號/上下文/wenxin
5 .
申請成為開發者##訪問http://mp.weixin.qq.com ,開啟開發者模式。填入URL和taken,申請成功,如下圖:
以上是JAVA微信開發新手必看的接取指南介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!