微信开发总结 二

1.发送,接受消息的常量

public class MessageConstant {
	
	/**文本 */ 
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";  
    /**图片*/
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";  
    /**语音*/
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice"; 
    /**视频*/
    public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
    /**小视频*/
    public static final String REQ_MESSAGE_TYPE_SHORTVIDEO = "shortvideo";
    /**地理位置*/ 
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";  
    /**链接消息*/ 
    public static final String REQ_MESSAGE_TYPE_LINK = "link";  
  
    
    /** 事件类型:subscribe(订阅)*/  
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";  
    /**事件类型:unsubscribe(取消订阅)*/  
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe"; 
    /**二维码扫描*/ 
    public static final String EVENT_TYPE_SCAN = "SCAN";  
    /**位置类型*/
    public static final String EVENT_TYPE_LOCATION  = "LOCATION";  
    /**事件类型:CLICK(自定义菜单点击事件)*/  
    public static final String EVENT_TYPE_CLICK = "CLICK";  
    
    /**文本 */ 
    public static final String RES_MESSAGE_TYPE_TEXT = "text";
    /**图片*/
    public static final String RES_MESSAGE_TYPE_IMAGE = "image";  
    /**语音*/
    public static final String RES_MESSAGE_TYPE_VOICE = "voice"; 
    /**视频*/
    public static final String RES_MESSAGE_TYPE_VIDEO = "video";
    /**音乐*/
    public static final String RES_MESSAGE_TYPE_MUSIC = "music";
    /**图文*/
    public static final String RES_MESSAGE_TYPE_NEWS = "news";
    
}



messageUtil:处理json

public class MessageUtil {
	/**
	 * 解析微信发来的请求(xml)
	 * @author wuyw
	 * 2015-7-29下午6:11:41
	 * @return
	 */
	public static Map<String,String> parseXml(HttpServletRequest request){
		Map<String,String> map = new HashMap<String,String>();
		try {
			//获取微信传来的参数
			InputStream inputStream = request.getInputStream();
			//读取输入流
			SAXReader reader = new SAXReader();
			Document document = reader.read(inputStream);
			//获取xml根元素
			Element root = document.getRootElement();
			@SuppressWarnings("unchecked")
			List<Element> elements = root.elements();
			//遍历获取的元素
			for (Element element : elements) {
				map.put(element.getName(), element.getText());
			}
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 扩展xstream令其支持CDATA
	 */
	public static XStream xStream=new XStream(new XppDriver(){
		public HierarchicalStreamWriter createWriter(Writer out){
			return new PrettyPrintWriter(out){
				//对所有xml增加CDATA标记
				boolean cdata=true;
				
				@SuppressWarnings("rawtypes")
				public void startNode(String name, Class clazz){
					super.startNode(name, clazz);
				}
				
				protected void writeText(QuickWriter writer, String text){
					if(cdata){
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					}else{
						writer.write(text);
					}
				}
			};
		}
	});
	//文本
	public static String messageToXml(BaseResMsg resMsg){
		xStream.alias("xml", resMsg.getClass());
		return xStream.toXML(resMsg);
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值