微信公众号消息接口开发——Java Servlet开发

张小龙搞出来的微信真的很牛逼啊,短短两三年就2亿用户量,这个数据似乎在警告QQ部门,IM软件上,你们QQ的老大位置并不稳固,仍需努力。当然QQ和微信不是一个平台上的产品,并不存在很大的竞争问题。

微信公众号消息接口API只有相当于只有一个简单调用链接,但是却完全改变了最初微信公众号的概念。不过说实话,这个接口真的设计的不怎么的...验证过程个字符处理啊,加密啊,拼接啊毫无用处,在申请页面填写的token也毫无用处,这些都是题外话了。不管这个接口腾讯设计得好不好,我们需要关心的是我们利用这个接口能够做什么,怎么做?

废话少说,言归正传,微信官网给我们提供了PHP的例子,所以做PHP的同学只需要画瓢即可,对于Java平台,网上还没有人说起,欲知Java平台怎么开发微信公众号接口,请看下文:

下面列出使用Java Servlet开发的一个简单的微信公众号接口调用的例子,如果有疑问,可以参见我本博的微信公众号:红色石头。

另外,我想大家一定很想知道《模拟登录微信公众平台,主动推送图文消息给用户》的有关信息。点开即可看到了~~~
redstones_wx_430.jpg

 
package com.wzwcxl.pubwx.hustac.pubwx;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.wzwcxl.pubwx.hustac.util.StringUtil;

/**
* 微信公众号接口调用Java版
* @author Xewee.Zhiwei.Wang
* @version 2012-12-17 上午9:16:02
* @Contract wzwahl36@QQ.com or http://wzwahl36.net/
*/
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("----------get---------------");
// response.getWriter().write(request.getParameter("echostr"));
doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("----------post---------------");
try {
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 < size) {
// 每次实际读取长度存于rbyte中
rbyte = is.read(buffer);
for(int i=0;i<rbyte;i++) {
xmldataByte[count + i] = buffer[i];
}
count += rbyte;
}
is.close();
String requestStr = new String(xmldataByte, "UTF-8");
Document doc = DocumentHelper.parseText(requestStr);
Element rootElt = doc.getRootElement();
String content = rootElt.elementText("Content");
String toUserName = rootElt.elementText("ToUserName");
String fromUserName = rootElt.elementText("FromUserName");
//得到所有的有用数据
System.out.println(content+ ":" + toUserName + ":" + fromUserName);
//文本消息
if (! StringUtil.isBlank(content) && "text".equals(content)) {
String responseStr = "<xml>";
responseStr += "<ToUserName><![CDATA[" + fromUserName
+ "]]></ToUserName>";
responseStr += "<FromUserName><![CDATA[" + toUserName
+ "]]></FromUserName>";
responseStr += "<CreateTime>" + System.currentTimeMillis()
+ "</CreateTime>";
responseStr += "<MsgType><![CDATA[text]]></MsgType>";
responseStr += "<Content>输入text或者news返回相应类型的消息,另外推荐你关注 '红色石头'(完全采用Java完成),反馈和建议请到http://wzwahl36.net</Content>";
responseStr += "<FuncFlag>0</FuncFlag>";
responseStr += "</xml>";
response.getWriter().write(responseStr);
}
//图文消息
else if (! StringUtil.isBlank(content) && "news".equals(content)) {
String responseStr = "<xml>";
responseStr += "<ToUserName><![CDATA[" + fromUserName
+ "]]></ToUserName>";
responseStr += "<FromUserName><![CDATA[" + toUserName
+ "]]></FromUserName>";
responseStr += "<CreateTime>" + System.currentTimeMillis()
+ "</CreateTime>";
responseStr += "<MsgType><![CDATA[news]]></MsgType>";
responseStr += "<Content><![CDATA[]]></Content>";

responseStr += "<ArticleCount>2</ArticleCount>";

responseStr += "<Articles>";
responseStr += "<item>";
responseStr += "<Title><![CDATA[图文消息——红色石头]]></Title>";
responseStr += "<Discription><![CDATA[图文消息正文——红色石头]]></Discription>";
responseStr += "<PicUrl><![CDATA[http://redstones.sinaapp.com/res/images/redstones_wx_258.jpg]]></PicUrl>";
responseStr += "<Url><![CDATA[http://redstones.sinaapp.com/]]></Url>";
responseStr += "</item>";

responseStr += "<item>";
responseStr += "<Title><![CDATA[图文消息——红色石头]]></Title>";
responseStr += "<Discription><![CDATA[图文消息正文——红色石头]]></Discription>";
responseStr += "<PicUrl><![CDATA[http://redstones.sinaapp.com/res/images/redstones_wx_258.jpg]]></PicUrl>";
responseStr += "<Url><![CDATA[http://redstones.sinaapp.com/]]></Url>";
responseStr += "</item>";

responseStr += "</Articles>";
responseStr += "<FuncFlag>1</FuncFlag>";
responseStr += "</xml>";
response.getWriter().write(responseStr);
}
//不能识别
else {
String responseStr = "<xml>";
responseStr += "<ToUserName><![CDATA[" + fromUserName
+ "]]></ToUserName>";
responseStr += "<FromUserName><![CDATA[" + toUserName
+ "]]></FromUserName>";
responseStr += "<CreateTime>" + System.currentTimeMillis()
+ "</CreateTime>";
responseStr += "<MsgType><![CDATA[text]]></MsgType>";
responseStr += "<Content>输入text或者news返回相应类型的消息,另外推荐你关注 '红色石头'(完全采用Java完成),反馈和建议请到http://wzwahl36.net</Content>";
responseStr += "<FuncFlag>0</FuncFlag>";
responseStr += "</xml>";
response.getWriter().write(responseStr);
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
需要注意是一点是:在验证链接时,将get方法写为以下:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("----------get---------------");
response.getWriter().write(request.getParameter("echostr"));
// doPost(request, response);
}
验证通过之后,将get方法和post方法写成一样的。
另外注意servlet的编码问题,我这个servlet里面没有写编码是因为我设置了编码过滤器:
package com.wzwcxl.pubwx.hustac.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* Servlet Filter implementation class EncodingFilter
*/
public class EncodingFilter implements Filter {

/**
* Default constructor.
*/
public EncodingFilter() {
// TODO Auto-generated constructor stub
}

/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}

/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
System.out.println("EncodingFilter: UTF-8");

chain.doFilter(request, response);
}

/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
附加:很多人问到代码中的StringUtil类中的isBlank方法是什么?然后要我发给他,其实StringUtil只有isBlank方法,这个方法就是用来判断字符串是不是空,所谓字符串为空,就是等于null或者trim之后为"",每次问到这个我都不知道怎么回答,所以在这里附加说明一下~

Enjoy~有空点点广告,感谢~~~

0 个评论

要回复文章请先登录注册