打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
微信开发总结 二

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

  1. public class MessageConstant {  
  2.       
  3.     /**文本 */   
  4.     public static final String REQ_MESSAGE_TYPE_TEXT = "text";    
  5.     /**图片*/  
  6.     public static final String REQ_MESSAGE_TYPE_IMAGE = "image";    
  7.     /**语音*/  
  8.     public static final String REQ_MESSAGE_TYPE_VOICE = "voice";   
  9.     /**视频*/  
  10.     public static final String REQ_MESSAGE_TYPE_VIDEO = "video";  
  11.     /**小视频*/  
  12.     public static final String REQ_MESSAGE_TYPE_SHORTVIDEO = "shortvideo";  
  13.     /**地理位置*/   
  14.     public static final String REQ_MESSAGE_TYPE_LOCATION = "location";    
  15.     /**链接消息*/   
  16.     public static final String REQ_MESSAGE_TYPE_LINK = "link";    
  17.     
  18.       
  19.     /** 事件类型:subscribe(订阅)*/    
  20.     public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";    
  21.     /**事件类型:unsubscribe(取消订阅)*/    
  22.     public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";   
  23.     /**二维码扫描*/   
  24.     public static final String EVENT_TYPE_SCAN = "SCAN";    
  25.     /**位置类型*/  
  26.     public static final String EVENT_TYPE_LOCATION  = "LOCATION";    
  27.     /**事件类型:CLICK(自定义菜单点击事件)*/    
  28.     public static final String EVENT_TYPE_CLICK = "CLICK";    
  29.       
  30.     /**文本 */   
  31.     public static final String RES_MESSAGE_TYPE_TEXT = "text";  
  32.     /**图片*/  
  33.     public static final String RES_MESSAGE_TYPE_IMAGE = "image";    
  34.     /**语音*/  
  35.     public static final String RES_MESSAGE_TYPE_VOICE = "voice";   
  36.     /**视频*/  
  37.     public static final String RES_MESSAGE_TYPE_VIDEO = "video";  
  38.     /**音乐*/  
  39.     public static final String RES_MESSAGE_TYPE_MUSIC = "music";  
  40.     /**图文*/  
  41.     public static final String RES_MESSAGE_TYPE_NEWS = "news";  
  42.       
  43. }  



messageUtil:处理json

  1. public class MessageUtil {  
  2.     /** 
  3.      * 解析微信发来的请求(xml) 
  4.      * @author wuyw 
  5.      * 2015-7-29下午6:11:41 
  6.      * @return 
  7.      */  
  8.     public static Map<String,String> parseXml(HttpServletRequest request){  
  9.         Map<String,String> map = new HashMap<String,String>();  
  10.         try {  
  11.             //获取微信传来的参数  
  12.             InputStream inputStream = request.getInputStream();  
  13.             //读取输入流  
  14.             SAXReader reader = new SAXReader();  
  15.             Document document = reader.read(inputStream);  
  16.             //获取xml根元素  
  17.             Element root = document.getRootElement();  
  18.             @SuppressWarnings("unchecked")  
  19.             List<Element> elements = root.elements();  
  20.             //遍历获取的元素  
  21.             for (Element element : elements) {  
  22.                 map.put(element.getName(), element.getText());  
  23.             }  
  24.             inputStream.close();  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.         return map;  
  29.     }  
  30.       
  31.     /** 
  32.      * 扩展xstream令其支持CDATA 
  33.      */  
  34.     public static XStream xStream=new XStream(new XppDriver(){  
  35.         public HierarchicalStreamWriter createWriter(Writer out){  
  36.             return new PrettyPrintWriter(out){  
  37.                 //对所有xml增加CDATA标记  
  38.                 boolean cdata=true;  
  39.                   
  40.                 @SuppressWarnings("rawtypes")  
  41.                 public void startNode(String name, Class clazz){  
  42.                     super.startNode(name, clazz);  
  43.                 }  
  44.                   
  45.                 protected void writeText(QuickWriter writer, String text){  
  46.                     if(cdata){  
  47.                         writer.write("<![CDATA[");  
  48.                         writer.write(text);  
  49.                         writer.write("]]>");  
  50.                     }else{  
  51.                         writer.write(text);  
  52.                     }  
  53.                 }  
  54.             };  
  55.         }  
  56.     });  
  57.     //文本  
  58.     public static String messageToXml(BaseResMsg resMsg){  
  59.         xStream.alias("xml", resMsg.getClass());  
  60.         return xStream.toXML(resMsg);  
  61.     }  
  62.       
  63. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
微信公众账号开发教程第4篇-消息及消息处理工具的封装
源码分析(1.4万字) | Mybatis接口没有实现类为什么可以执行增删改查
XML与JSON学习笔记分享
xstream实现JAVA对象和XML的相互转化
xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置
String和InputStream的转换
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服