微信开发总结 二
2015-08-03 12:34
363 查看
1.发送,接受消息的常量
messageUtil:处理json
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); } }
相关文章推荐
- 微信开发总结 一
- 微信开发总结
- 人人,金山西山居,腾讯互娱,微信,网易游戏offer及面经(转)
- Android 微信下拉眼睛
- android 微信支付直接跳转结果
- Java微信公众号支付
- 如何利用事件营销一天获得35万微信粉丝
- 微信公众平台开发(十二) 发送客服消息
- 微信公众平台开发(73) 客服接口发送客服消息
- 关于微信分享、登录的一些注意事项
- 好消息: 《微信商城开发实战》 已经由电子工业出版社出版发行啦
- 使用AccessibilityService实现微信自动抢红包
- 用MVC结构写web小程序时出现“ Cannot forward after response has been committed”的错误
- 微信在召唤千万个“王尼玛”
- 判断闰年平年的小程序
- java 微信开发
- 微信JS SDK使用权限签名算法 jsapi_ticket
- ICCMO微信公众账号开发系列(3)CURL模拟登陆教务系统查成绩
- 【News】SpagoBI中国官方微信对外发布
- 企业微信认证需要什么资料(最新认证流程)