微信公众号--被动回复用户消息

https://www.bilibili.com/video/BV1nb411P76t?p=7

以图文消息为例

1、创建对应的实体:

 1 public class ResponseMessage {
 2      private String toUserName;
 3      private String fromUserName;
 4      private String createTime;
 5      public ResponseMessage(Map<String, String> requestMap) { // 这个是用户发送的消息的一个Map
 6          this.toUserName = requestMap.get("FromUserName"); 
 7          this.fromUserName = requestMap.get("ToUserName");
 8          this.createTime = System.currentTimeMillis()/1000+"";
 9      }
10 }
11 public class NewsResponseMessage extends ResponseMessage {
12     private String msgType;
13     private String articleCount;
14     private List<Article> articles = new ArrayList<>();
15     private Article article;
16     public NewsResponseMessage(Map requestMap, List<Article> articles) {
17         super(requestMap);
18         articles = articles;
19         articleCount = "" + articles.size() + "";
20         msgType = "news"; 
21     }
22 }    
23 public class Article {
24     private String title;
25     private String description;
26     private String picUrl;
27     private String url;
28     public Article(String title, String description, String picUrl, String url) {
29         this.title = title;
30         this.description = description;
31         this.picUrl = picUrl;
32         this.url = url;
33     }
34 }

 

2、将NewsResponseMessage类转化成对应的xml

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html  微信开发文档 

使用@XStreamAlias()方式

准备工作:需要三个jar包: xmlpull  xpp3   xstream

链接:https://pan.baidu.com/s/12QM_INm7A26ZxolBlf1JXA  提取码:xdep 

对照图文消息的xml的格式,在对应的实体上加上注解@XStreamAlias("")

 1 public class ResponseMessage {
 2     @XStreamAlias("ToUserName") // 这个名称就是在xml里面会显示的标签的名称
 3     private String toUserName;
 4     @XStreamAlias("FromUserName")
 5     private String fromUserName;
 6     @XStreamAlias("CreateTime")
 7     private String createTime;
 8     public ResponseMessage(Map<String, String> requestMap) {
 9         this.toUserName = requestMap.get("FromUserName");  
10         this.fromUserName = requestMap.get("ToUserName");
11         this.createTime = System.currentTimeMillis()/1000+"";
12     }
13 }
14 
15 @XStreamAlias("xml")  // 要将这个类转换成xml格式   写在类外面的这个 会是转换成xml格式后的最外面的大标签
16 public class NewsResponseMessage extends ResponseMessage {
17     @XStreamAlias("ArticleCount")
18     private String articleCount;
19     @XStreamAlias("Articles") // 
20     private List<Article> articles = new ArrayList<>();
21     private Article article;
22         @XStreamAlias("MsgType")
23         private String msgType
24     public NewsResponseMessage(Map requestMap, List<Article> articles) {
25         super(requestMap);
26         articles = articles;
27         articleCount = "" + articles.size() + "";
28                 msgType = "news";
29     }
30 }    
31 
32 @XStreamAlias("item")
33 public class Article {
34     @XStreamAlias("Title")
35     private String title;
36     @XStreamAlias("Description")
37     private String description;
38     @XStreamAlias("PicUrl")
39     private String picUrl;
40     @XStreamAlias("Url")
41     private String url;
42 
43     public Article(String title, String description, String picUrl, String url) {
44         this.title = title;
45         this.description = description;
46         this.picUrl = picUrl;
47         this.url = url;
48     }
49 }

3、将实例转换成xml:

1 public static String beanToXml(ResponseMessage ResponseMessage) {
2     XStream xStream = new XStream();
3     // 在解析之前先开启注解
4     xStream.processAnnotations(NewsResponseMessage.class);
5     String xml = xStream.toXML(ResponseMessage);
6     return xml;
7 }

 

posted @ 2020-06-01 22:00  DDiamondd  阅读(768)  评论(0编辑  收藏  举报
TOP