微信开发二三事:走进微信

上一篇说到已经成功成为开发者了,所以,现在想干嘛就干嘛,开发者嘛,程序猿,都是有很强的控制欲的~~

当初成为开发者后,就在看那个微信公众平台的开发者文档,看接收信息和发送信息,发现其实都是xml格式的,根节点都是<xml>,其后就是一些个属性,比如

FromUserName、ToUserName....,那我们去解析微信发过来的xml的话,就能得到我们想要的数据了(注:这是文本信息的数据ToUserName、FromUserName、

CreateTime、MsgType、Conten、MsgId)。其中的CreateTime是指1970年1月1号0时0分0秒到消息发送的时候经历了多少秒,我们所应该注意的,是ToUserName、

FromUserName、MsgType和Conten,因为如果我们得到这四个值,就能发送信息给微信用户了。

OK,那我们先来看看如何去解析微信发过来的xml。通过查看文档,我们知道,微信服务器是通过POST请求,把消息的XML数据包到开发者填写的URL上,so,我们就可以

根据servlet中的dopost方法去获取,方法如下:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		InputStream is = request.getInputStream();
		try {
			PushManage push = new PushManage();
			String getXml = push.PushManageXml(is);
			out.print(getXml);
		} catch (JDOMException e) {
			out.print("请求失败!");
		}
	}
因为我们是用jdom去解析的,所以是获取io流去解析的,PushManager是我创建的解析的一个类,代码如下:

/**
 * 微信所有接口入口
 * 
 * @author tane 
 */

public class PushManage {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PushManage.class);
	
	private TypeInfoBiz typeInfoBiz=new TypeInfoBizImpl();
	private CreateXmlUtil createXmlUtil=new CreateXmlUtil();
	
	public String PushManageXml(InputStream is) throws JDOMException {
		String returnStr = "";
		String toName = "";
		String FromName = "";
		String type = "";
		String content = "";//回送文本信息内容
		String con = "";//接收文本信息内容
		String picUrl = "";//回送图片链接
		String pic = "";//接收图片链接
		String mediaId = "";//图片消息媒体id
		String event = "";// 自定义按钮事件请求
		String eKey = "";// 事件请求key值
		String format="";//音乐的语音格式
		String location_X="";//地理位置维度
		String location_Y="";//地理位置经度
		String scale="";//地图缩放大小
		String label="";//地理位置信息
		String url="";//图文信息回复的图片地址
		String ss="";//图文信息回复的文章地址
		String linkTitle="";//链接标题
		String linkDescription="";//链接描述
		String linkUrl="";//链接地址
		
		String Recognition="";	//语音识别结果

		try {
			SAXBuilder sax = new SAXBuilder();<span style="white-space:pre">		</span>//注意,用的是jdom解析
			Document doc = sax.build(is);

			// 获得文件的根元素
			Element root = doc.getRootElement();

			// 获得根元素的第一级子节点
			List list = root.getChildren();
			for (int j = 0; j < list.size(); j++) {
				// 获得结点
				Element first = (Element) list.get(j);
				if (first.getName().equals("ToUserName")) {
					toName = first.getValue().trim();
				} else if (first.getName().equals("FromUserName")) {
					FromName = first.getValue().trim();
				} else if (first.getName().equals("MsgType")) {
					type = first.getValue().trim();
				} else if (first.getName().equals("Content")) {
					con = first.getValue().trim();
				} else if (first.getName().equals("PicUrl")){
					pic = first.getValue().trim();
				} else if (first.getName().equals("MediaId")){
					mediaId = first.getValue().trim();
				} else if (first.getName().equals("Event")) {
					event = first.getValue().trim();
				} else if (first.getName().equals("EventKey")) {
					eKey = first.getValue().trim();
				} else if (first.getName().equals("Format")){
					format = first.getValue().trim();
				} else if (first.getName().equals("Location_X")){
					location_X = first.getValue().trim();
				} else if (first.getName().equals("Location_Y")){
					location_Y = first.getValue().trim();
				} else if (first.getName().equals("Scale")){
					scale = first.getValue().trim();
				} else if (first.getName().equals("Label")){
					label = first.getValue().trim();
				} else if (first.getName().equals("Title")){
					linkTitle = first.getValue().trim();
				} else if (first.getName().equals("Description")){
					linkDescription = first.getValue().trim();
				} else if (first.getName().equals("Url")){
					linkUrl = first.getValue().trim();
				} else if (first.getName().equals("Recognition")){
					Recognition = first.getValue().trim();
				} 
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (type.equals("event")) {//关注我
			if (event.equals("subscribe")) {
				//表示单纯的关注
				if(eKey==null||"".equals(eKey)){
					returnStr = "欢迎关注我~~";
				}
			}	
		} else if (type.equals("text")) {//文本信息
			returnStr = typeInfoBiz.TypeOfText(toName, FromName, con,type);//普通文本信息回复
		} else if (type.equals("image")) {//图片信息信息
			content="图片链接为:"+pic+"\n图片ID为:"+mediaId;
			returnStr = typeInfoBiz.TypeOfText(toName, FromName, content,type);
		} else if(type.equals("voice")){
			returnStr = typeInfoBiz.TypeOfText(toName, FromName, Recognition,type);<span style="white-space:pre">	</span>//把语音识别的结果传过去
		} else if(type.equals("location")){
			content="地理纬度是:\n"+location_X+"\n"+"地理经度是:\n"+location_Y+"\n"+"地图缩放大小是:"+scale+"\n"+"地理位置信息"+label;
			returnStr = typeInfoBiz.TypeOfText(toName, FromName, content,type);
		} else if(type.equals("link")){
			content="链接标题是:\n"+linkTitle+"\n"+"链接内容是:\n"+linkDescription+"\n"+"链接地址是:\n"+linkUrl;
			returnStr = typeInfoBiz.TypeOfText(toName, FromName, content,type);
		}
		return returnStr;
	}
然后在typeInfoBiz里面去判断类型,根据类型或者内容去返回信息,如下:

package com.tane.biz;

/**
 * 
 * @author tane
 */
public interface TypeInfoBiz {
	/**
	 * 回复文本信息,这里的content都是从数据库里面查出
	 * @param toName
	 * @param FromName
	 * @param content   用户传过来的内容
	 * @param type    消息类别
	 * @return
	 */
	public String TypeOfText(String toName,String FromName,String content,String type);
}
/**
 * @author tane
 *
 */
public class TypeInfoBizImpl extends DBHelper implements TypeInfoBiz {
	
	private CreateXmlUtil createXmlUtil=new CreateXmlUtil();/*创建回复信息的帮助类*/
	private String returnStr="";

	/**
	 * 回复文本
	 */
	public String TypeOfText(String toName, String FromName, String content, String type) {
<span style="white-space:pre">		</span>if("text".equals(type)){<span style="white-space:pre">		</span>//这里之所以做判断,是为以后区分信息做准备
			returnStr=createXmlUtil.getBackXMLTypeText(toName, FromName, content);
		}else if("voice".equals(type)){
			returnStr=createXmlUtil.getBackXMLTypeText(toName, FromName, content);
		}else if("image".equals(type)){
			returnStr=createXmlUtil.getBackXMLTypeText(toName, FromName, content);
		}else{
			returnStr=createXmlUtil.getBackXMLTypeText(toName, FromName, content);
		}
		
		return returnStr;
	}}
CreateXMLUtil是用来创建xml的,其中包括很多种类型,比如文本信息、音乐、图文等等,我这里只列出文本信息

public class CreateXmlUtil {
	/**
	 * 文本信息回复
	 * 
	 * @param toName
	 * @param FromName
	 * @param content
	 * @return
	 */
	public String getBackXMLTypeText(String toName, String FromName,
			String content) {
		String returnStr = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String times = format.format(new Date());
		Element rootXML = new Element("xml");
		rootXML.addContent(new Element("ToUserName").setText(FromName));
		rootXML.addContent(new Element("FromUserName").setText(toName));
		rootXML.addContent(new Element("CreateTime").setText(times));
		rootXML.addContent(new Element("MsgType").setText("text"));
		rootXML.addContent(new Element("Content").setText(content));
		Document doc = new Document(rootXML);
		XMLOutputter XMLOut = new XMLOutputter();
		returnStr = XMLOut.outputString(doc);
		return returnStr;
	}

ok,至此,文本信息的接收和返回已经写完,但这也只是文本信息,还有其他的音乐、图文、图片等可以接收和返回,我这里也只是列出文本信息,希望大家可以根据文本信息,自己写出其他类型的信息接收和发送,明天我们来讲讲自定义菜单的使用




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值