标签:on() data 接收 attr utf-8 rate equal pen 图片
1 /** 2 * 处理微信服务器发来的消息 3 */ 4 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 5 { 6 // TODO 消息的接收、处理、响应 7 System.out.println("接口调用了一次"); 8 9 String ret = IOUtils.toString(request.getInputStream(), "utf-8").toString(); 10 System.out.println(ret); 11 12 WeChatMsgBL tWeChatMsgBL = new WeChatMsgBL(); 13 String result = tWeChatMsgBL.dealWechatMsg(ret); 14 15 response.setCharacterEncoding("UTF-8"); 16 17 // 返回文本消息,将处理结果返回 18 PrintWriter out = response.getWriter(); 19 20 System.out.println(result); 21 22 out.print(result); 23 24 out.close(); 25 26 }
可以先输出接收到的xml信息:
第二步,解析及处理接收到数据的内容:
1 /** 2 * 微信消息主要类 3 * @author user 4 */ 5 public class WechatMsg 6 { 7 // 开发者微信号 8 private String ToUserName = ""; 9 10 // 发送方帐号(一个OpenID) 11 private String FromUserName = ""; 12 13 // 消息创建时间 (整型) 14 private String CreateTime = ""; 15 16 // 消息类型 17 private String MsgType = ""; 18 19 // 消息id,64位整型 20 private String MsgId = ""; 21 22 public String getToUserName() 23 { 24 return ToUserName; 25 } 26 27 public void setToUserName(String toUserName) 28 { 29 ToUserName = toUserName; 30 } 31 32 public String getFromUserName() 33 { 34 return FromUserName; 35 } 36 37 public void setFromUserName(String fromUserName) 38 { 39 FromUserName = fromUserName; 40 } 41 42 public String getCreateTime() 43 { 44 return CreateTime; 45 } 46 47 public void setCreateTime(String createTime) 48 { 49 CreateTime = createTime; 50 } 51 52 public String getMsgType() 53 { 54 return MsgType; 55 } 56 57 public void setMsgType(String msgType) 58 { 59 MsgType = msgType; 60 } 61 62 public String getMsgId() 63 { 64 return MsgId; 65 } 66 67 public void setMsgId(String msgId) 68 { 69 MsgId = msgId; 70 } 71 72 73 74 }
1 /** 2 * 普通消息类 3 * @author Damon 4 */ 5 public class WechatTextMsg extends WechatMsg 6 { 7 8 // 文本内容 9 private String Content = ""; 10 11 public String getContent() 12 { 13 return Content; 14 } 15 16 public void setContent(String content) 17 { 18 this.Content = content; 19 } 20 }
1 /** 2 * 微信消息处理类 3 * @author Damon 4 */ 5 public class WeChatMsgBL 6 { 7 8 /** 9 * 处理微信信息 10 * @param reqInfo xml信息数据 11 * @return 处理结果 12 */ 13 public String dealWechatMsg(String reqInfo) 14 { 15 16 String ret = reqInfo; 17 18 String result = ""; 19 20 WechatTextMsg tReturnMsg = new WechatTextMsg(); 21 22 try 23 { 24 Document returndoc = DocumentHelper.parseText(ret); 25 26 Element rootElement = returndoc.getRootElement(); 27 28 String tMsgType = rootElement.elementText("MsgType"); 29 30 // 根据MsgType 返回具体的消息类 31 32 if (WeChatUtil.TEXT_MSG_TYPE.equals(tMsgType)) 33 { 34 result = dealTextMsg(rootElement); 35 } 36 37 // 封装返回信息 38 tReturnMsg.setCreateTime(new Date().getTime() + ""); 39 tReturnMsg.setToUserName(rootElement.elementText("FromUserName")); 40 tReturnMsg.setFromUserName(rootElement.elementText("ToUserName")); 41 tReturnMsg.setContent(result); 42 tReturnMsg.setMsgType(WeChatUtil.TEXT_MSG_TYPE); 43 // 将返回结果封装成文本消息 44 result = WeChatUtil.textToXML(tReturnMsg); 45 } 46 catch (DocumentException e) 47 { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 52 return result; 53 } 54 55 /** 56 * 处理文本消息 57 * @param msg 58 * @return 59 */ 60 private String dealTextMsg(Element rootElement) 61 { 62 63 WechatTextMsg tWechatTextMsg = new WechatTextMsg(); 64 // 依次赋值 65 tWechatTextMsg.setCreateTime(rootElement.elementText("CreateTime")); 66 tWechatTextMsg.setFromUserName(rootElement.elementText("FromUserName")); 67 tWechatTextMsg.setToUserName(rootElement.elementText("ToUserName")); 68 tWechatTextMsg.setMsgType(rootElement.elementText("MsgType")); 69 tWechatTextMsg.setMsgId(rootElement.elementText("MsgId")); 70 tWechatTextMsg.setContent(rootElement.elementText("Content")); 71 72 System.out.println("传入的消息是:" + tWechatTextMsg.getContent()); 73 74 // 后续可处理保存到数据库及数据分析等,现在直接返回结果 75 76 // 将数据转存至数据库 77 // Connection conn = DBConnPool.getConnection(); 78 79 return "传入的文本消息处理完毕"; 80 } 81 82 }
1 // 定义微信消息类型--文本 2 public static final String TEXT_MSG_TYPE = "text";
1 /** 2 * 将对象转为XML输出 3 * @param textMsg 4 * @return 5 */ 6 public static String textToXML(WechatTextMsg textMsg) 7 { 8 xstream.alias("xml", textMsg.getClass()); 9 return xstream.toXML(textMsg); 10 11 } 12 13 14 /** 15 * 扩展xstream使其支持CDATA 16 */ 17 private static XStream xstream = new XStream(new XppDriver() 18 { 19 public HierarchicalStreamWriter createWriter(Writer out) 20 { 21 return new PrettyPrintWriter(out) 22 { 23 // 对所有xml节点的转换都增加CDATA标记 24 boolean cdata = true; 25 26 @SuppressWarnings("unchecked") 27 public void startNode(String name, Class clazz) 28 { 29 super.startNode(name, clazz); 30 } 31 32 protected void writeText(QuickWriter writer, String text) 33 { 34 if (cdata) 35 { 36 writer.write("<![CDATA["); 37 writer.write(text); 38 writer.write("]]>"); 39 } 40 else 41 { 42 writer.write(text); 43 } 44 } 45 }; 46 } 47 });
标签:on() data 接收 attr utf-8 rate equal pen 图片
原文地址:http://www.cnblogs.com/cooldamon/p/7208622.html