微信接入java示例 (使用了jfinal,其他框架自己修改就好)(转)

代码地址: 
代码使用了jdom解析和输出xml 
https://github.com/0x0001/java-weixinlib

 

public class WxCtr1 extends Controller {
	
	private static final String TOKEN = "xxxxxx";
	
	public void index() {
		String signature = getPara("signature");
		String timestamp = getPara("timestamp");
		String nonce = getPara("nonce");
		String echostr = getPara("echostr");
		
		if(null != timestamp && null != nonce && null != echostr && null != signature) {
			if(WeiXin.access(TOKEN, signature, timestamp, nonce)) {
				renderHtml(echostr);
				return;
			}
			renderNull();
			return;
		}
		
		try {
			WxRecvMsg msg = WeiXin.recv(getRequest().getInputStream());
			WxSendMsg sendMsg = WeiXin.builderSendByRecv(msg);
			System.out.println(msg);
			
			if(msg instanceof WxRecvEventMsg) {
				WxRecvEventMsg m = (WxRecvEventMsg) msg;
				String event = m.getEvent();
				if("subscribe".equals(event)) {
					String content = "感谢关注";
					sendMsg = new WxSendTextMsg(sendMsg, content);
					WeiXin.send(sendMsg, getResponse().getOutputStream());
					renderNull();
				}
			}
			
			if(msg instanceof WxRecvTextMsg) {
				WxRecvTextMsg m = (WxRecvTextMsg) msg;
				
				String text = m.getContent();
				if(null != text) 
					text = text.trim();
				
				Set<String> keyworkds = new HashSet<String>();
				keyworkds.add("1");
				keyworkds.add("菜单");
				keyworkds.add("menu");
				
				
				if(keyworkds.contains(text)) {
					String content = "1. 菜单\n2.音乐\n,3.图文\n";
					sendMsg = new WxSendTextMsg(sendMsg, content);
					
				} else if("2".equals(text)) {
					sendMsg = new WxSendMusicMsg(sendMsg, "夜夜夜夜","林志炫",
							"http://hugemaxiao.co/COFFdD0xMzY1Njg5NTIyJmk9MTIxLjM0LjMxLjMmdT1Tb25ncy92MS9mYWludFFDLzQ3LzkxMGFlM2JjMzM5MDNlZGI3NmY3MDUzY2ZjZDI1YjQ3Lm1wMyZtPTMzMWQwNGY2YTNkNTRiNTMwYzc1MjhkN2FmNDRhMGZjJnY9ZG93biZuPdK50rnSudK5JnM9wdbWvuzFJnA9cw==.mp3", 
							"http://hugemaxiao.co/COFFdD0xMzY1Njg5NTIyJmk9MTIxLjM0LjMxLjMmdT1Tb25ncy92MS9mYWludFFDLzQ3LzkxMGFlM2JjMzM5MDNlZGI3NmY3MDUzY2ZjZDI1YjQ3Lm1wMyZtPTMzMWQwNGY2YTNkNTRiNTMwYzc1MjhkN2FmNDRhMGZjJnY9ZG93biZuPdK50rnSudK5JnM9wdbWvuzFJnA9cw==.mp3");
				} else if("3".equals(text)) {
					sendMsg = new WxSendNewsMsg(sendMsg)
					.addItem("图片4", "描述4", "http://g.hiphotos.baidu.com/album/w%3D230/sign=b2b8791418d8bc3ec60801c9b28ba6c8/1ad5ad6eddc451da5ff2c0c5b7fd5266d016329c.jpg", "http://www.oschina.net")
					.addItem("图片1", "描述1", "http://a.hiphotos.baidu.com/album/w%3D230/sign=67afac3d242dd42a5f0906a8333a5b2f/8c1001e93901213f088bb78855e736d12f2e952e.jpg", "http://www.google.com")
					.addItem("图片2", "描述2", "http://e.hiphotos.baidu.com/album/w%3D230/sign=5254e9265243fbf2c52ca120807eca1e/77c6a7efce1b9d1669f2564ff2deb48f8c5464e3.jpg", "http://www.baidu.com")
					.addItem("图片3", "描述3", "http://d.hiphotos.baidu.com/album/w%3D230/sign=c3b3fe3aaa18972ba33a07c9d6cc7b9d/3812b31bb051f81995bdc164dbb44aed2e73e700.jpg", "http://www.sina.com")
					;
				}  else {
					String content = "1. 菜单\n2.音乐\n,3.图文\n";
					sendMsg = new WxSendTextMsg(sendMsg, content);
				}
				WeiXin.send(sendMsg, getResponse().getOutputStream());
				renderNull();
				return;
			}  else {
				String content = "1. 菜单\n2.音乐\n,3.图文\n";
				sendMsg = new WxSendTextMsg(sendMsg, content);
				WeiXin.send(sendMsg, getResponse().getOutputStream());
				renderNull();
				return;
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		renderNull();
	}
}

 

 

学习了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值