您的位置:首页 > 移动开发 > 微信开发

微信公众平台API的Java通讯实现

2015-10-27 19:31 573 查看
代码简介
微信公众平台相信大家也不陌生,官方网站提供了一个简单的php程序Demo
因为微信平台采用HTTP方式承载微信的协议,而且不是双向通讯,也就是说只能由微信服务器主动请求我们的服务器。其实当你使用我开发的weixinapi包,你不用关心那么多的,呵呵。
看了api文档很久一段事件,今天突然恍然大雾,然后weixiwww.0871hx.comnapi的java版出来了

此api结合我之前从事通讯协议方面经验,采用Session抽象类方式+消息对象模式开发

实现了:
接收微信服务器的文本消息、图片消息、事件消息、链接消息、地理位置消息等功能,
调用callback()响应回复文本消息、音乐消息、图文消息 。

使用条件:有自己的服务器,公网IP+80端口。

欢迎大家关注我的微信公众号!!!(暂时没做功能,未来会有功能的,先关注到嘛,今天登录发现有个朋友发三条消息,还有一条图片里面一对情侣,由于他取消关注,我没能联系到他。)

注意:反编查看源码!

代码片段
package org.marker.weixin;

import javwww.lzfsk.coma.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.www.kmxxfk.comHttpServletResponse;

import org.marker.weixin.msg.Msg;
import org.marker.weixin.msg.Msg4Event;
import org.marker.weixin.msg.Msg4Image;
import org.marker.weixin.msg.Msg4Link;
import org.marker.weixin.msg.Msg4Location;
import org.marker.weixin.msg.Msg4Music;
import org.markerwww.64186418.com.weixin.msg.Msg4Text;

/**
* 处理微信服务器请求的Servlet URL地址:http://xxx/www.szbnmz.comweixin/dealwith.do
*
* 注意:官方文档限制使用80端口哦!
*
* @author marker
* @blog www.www.bnzrpf.comyl-blog.com
* @weibo http://t.qq.com/wuweiit */
public class WinXinServlet extends HttpServlet {
private static fwww.kmhxyyfk.cominal long serialVersionUID = 1L;

//TOKEN 是你在微信平台开发模式中设置的哦
public static final String TOKEN = "";

/**
* 处理微信服务器验证
*
* @see HttpServlet#dwww.kmhxnkyy.comoGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request,
HttpSerwww.kmhxnk.comvletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParamewww.kmhx120.comter("nonce");// 随机数
String echostr = requwww.gywcjb120.comest.getParameter("echostr");// 随机字符串

// 重写totring方法,得到三个参数的拼接字符串
List<String> list = new ArrayList<String>(3) {
private static final www.myfengxiang.comlong serialVersionUID = 2621444383666420433L;
public String toString() {
return this.get(0www.junkenk.net) + this.get(1) + this.get(2);
}
};
list.add(TOKEN);
list.add(timestamp);
list.add(www.gywczx.comnonce);
Collections.sort(list);// 排序
String tmpStr = new MySecurity().encode(list.toString(),
MySecurity.SHA_1);// SHA-1加密
Writer out = response.getWriter();
if (signature.equawww.bnzr120.comls(tmpStr)) {
out.write(echostr);// 请求验证成功,返回随机码
} else {
out.write("");
}
out.flush();
out.close();
}

/**
* 处理微信服务器发过来的各种消息,包括:文本、图片、地理位置、音乐等等
*
*
*/
protected void doPost(HttpServlewww.junkefk.comtRequest request,
HttpServletResponse response) throws ServletException, IOException {
InputStream is = request.getInputStream();
OutputStream os = response.getOutputStream();
@SuppressWarnings("unused")
Session session = new Session(is, os) {

@Override
public void onTextMsg(Msg4Text msg) {
System.ouwww.bnzrpf.comt.println(msg.getToUserName());
System.out.println("收到消息"+msg.getContent());

//回复一条消息
Msg4Text reMsg = new Msg4Text();
reMsg.setFromUserName(msg.getToUserName());
reMsg.setToUserName(msg.getFromUserName());
reMsg.setCreateTime(msg.getCreateTime());
reMsg.setMsgType(Msg.MSG_TYPE_TEXT);//设置消息类型
reMsg.setCwww.szbnmz.comontent("呵呵,谢谢您给我发消息");
reMsg.setFuncFlag("0");

callback(reMsg);//回传消息
}

@Override
public void onImageMsg(Msg4Image msg) {
System.out.println("收到图片"+msg.getPicUrl());
}

@Override
public void onEventMsg(Msg4Event msg) {
String eType = m
4000
sg.getEvent();
if(Msg4Event.SUBSCRIBE.equals(eType)){
System.out.println("关注人:"+msg.getFromUserName());

}else if(Msg4Event.UNSUBSCRIBE.equals(eType)){
System.out.println("取消关注人:"+msg.getFromUserName());

}else{//点击:暂时内测
System.out.println("反正有事件推过来");
}

}

@Override
public void onLinkMsg(Msg4Link msg) {
System.out.println("收到URL:"+msg.getUrl());
}

@Override
public void onLocationMsg(Msg4Location msg) {
System.out.println("收到地理位置消息:");
System.out.println("X:"+msg.getLocation_X());
System.out.println("Y:"+msg.getLocation_Y());
System.out.println("Scale:"+msg.getScale());
}

@Override
public void onErrorMsg(int errorCode) {
// TODO Auto-generated method stub

}

};
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: