微信公众平台java开发详解(工程代码+解析)

  • 时间:
  • 浏览:0
作者:匿名 hihi 来源: 浏览: 2014-8-21 15:15:23 字号:大 中 小

[摘要]本文对微信公众平台开发者模式进行了删剪的讲解,并对主要代码进行讲解分析,让初学者尽快上手。

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

  在阅读本文以后,应对微信公众平台的官方开发文档有所了解,知道接收和发送的也有xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是有4个 自然语言解析的开放平台,都都都可以 也能 帮当当人们 除理整个微信开发过程中最困难的问題,此处太多讲,下面会有其删剪的调用辦法 。

  1.1 在登录微信官方平台以后,开启开发者模式,此时都要当当人们 填写url和token,所谓url好多好多 当当人们 另一方服务器的接口,用WechatServlet.java来实现,相关解释不可能 在注释中说明,代码如下:

package demo.servlet;  

import java.io.BufferedReader;



import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import javax.servlet.ServletException;



import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import demo.process.WechatProcess;



/**

* 微信服务端收发消息接口

*

* @author pamchen-1

*

*/

public class WechatServlet extends HttpServlet {

/**



* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request

* the request send by the client to the server

* @param response

* the response send by the server to the client

* @throws ServletException

* if an error occurred

* @throws IOException

* if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

/** 读取接收到的xml消息 */



StringBuffer sb = new StringBuffer();

InputStream is = request.getInputStream();

InputStreamReader isr = new InputStreamReader(is, "UTF-8");

BufferedReader br = new BufferedReader(isr);

String s = "";

while ((s = br.readLine()) != null) {

sb.append(s);

}

String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据

String result = "";



/** 判断有无微信接入激活验证,都都都可以 也能 首次接入验证时才会收到echostr参数,此时都要把它直接返回 */

String echostr = request.getParameter("echostr");

if (echostr != null && echostr.length() > 1) {

result = echostr;

} else {

//正常的微信除理流程

result = new WechatProcess().processWechatMag(xml);

}

try {



OutputStream os = response.getOutputStream();

os.write(result.getBytes("UTF-8"));

os.flush();

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**



* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to

* post.

*

* @param request

* the request send by the client to the server

* @param response

* the response send by the server to the client

* @throws ServletException

* if an error occurred

* @throws IOException

* if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

  1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的一块儿,可自动生成web.xml中的配置。前面所提到的url处都都都可以 也能 填写类式:http;//服务器地址/项目名/wechat.do

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

<web-app version="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/1501/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>

<description>This is the description of my J2EE component</description>

<display-name>This is the display name of my J2EE component</display-name>

<servlet-name>WechatServlet</servlet-name>

<servlet-class>demo.servlet.WechatServlet</servlet-class>

</servlet>

<servlet-mapping>



<servlet-name>WechatServlet</servlet-name>

<url-pattern>/wechat.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

  1.3 通过以上代码,当当人们 不可能 实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这有4个 步骤。

快捷键使用:上一页“←”,下一页“→”

sssss
Tags: 微信公众平台   微信公众平台开发详解  
责任编辑:lsw16