WeiXinMessageReplyUtils.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.yihu.wlyy.wechat.util;
  2. import java.util.Date;
  3. import java.util.List;
  4. import java.util.Map;
  5. /**
  6. * 消息回复工具类
  7. *
  8. * Created by lyr on 2016/08/15.
  9. */
  10. public class WeiXinMessageReplyUtils {
  11. /**
  12. * 回复图文消息
  13. *
  14. * @param toUser
  15. * @param fromUser
  16. * @param articles
  17. * @return
  18. * @throws Exception
  19. */
  20. public static String replyNewsMessage(String toUser,String fromUser,List<Map<String,String>> articles) throws Exception {
  21. if(articles == null || articles.size() < 1){
  22. throw new Exception("图文信息不能为空!");
  23. }
  24. StringBuilder result = new StringBuilder();
  25. result.append("<xml>");
  26. result.append("<ToUserName>" + toUser + "</ToUserName>");
  27. result.append("<FromUserName>" + fromUser + "</FromUserName>");
  28. result.append("<CreateTime>" + new Date().getTime() + "</CreateTime>");
  29. result.append("<MsgType>news</MsgType>");
  30. result.append("<ArticleCount>" + articles.size() + "</ArticleCount>");
  31. result.append("<Articles>");
  32. for(Map<String,String> article : articles){
  33. result.append("<item>");
  34. result.append("<Title>" + article.get("Title") +"</Title>");
  35. result.append("<Description>" + article.get("Description") + "</Description>");
  36. result.append("<PicUrl>" + article.get("PicUrl") + "</PicUrl>");
  37. result.append("<Url>" + article.get("Url") + "</Url>");
  38. result.append("</item>");
  39. }
  40. result.append("</Articles>");
  41. result.append("</xml>");
  42. return result.toString();
  43. }
  44. }