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);
- }
-
- }
|