(12)被动回复图文消息

1.首先分析图文的数据结构
  • 创建三个pojo对象,他们之间的关系如图

列表内容

  • pojo的数据转化为xml格式的数据
<xml>
  <ToUserName>oHNZ-w5CL6LPMNWP3x5qUX92mZDo</ToUserName>
  <FromUserName>gh_60ef45a38569</FromUserName>
  <CreateTime>1504699423279</CreateTime>
  <MsgType>news</MsgType>
  <ArticleCount>2</ArticleCount>
  <Articles>
    <item>
      <Title>马云,何许人也?</Title>
      <Description>阿里巴巴集团创始人</Description>
      <PicUrl>http://2wh8ui.natappfree.cc/WinXin/wxSource/maYun.jpg</PicUrl>
      <Url>https://baike.baidu.com/item/%E9%A9%AC%E4%BA%91/6252?fr=aladdin</Url>
    </item>
    <item>
      <Title>马化腾,何许人也?</Title>
      <Description>企业家</Description>
      <PicUrl>http://2wh8ui.natappfree.cc/WinXin/wxSource/maHuaTeng.jpg</PicUrl>
      <Url>https://baike.baidu.com/item/%E9%A9%AC%E5%8C%96%E8%85%BE/124988?fr=aladdin</Url>
    </item>
  </Articles>
</xml>
2.创建工具类,目的返回符合要求的xml数据
  • sendArticlesMessage,创建发送图文消息的信息
public static String sendArticlesMessage(String toUserName,String fromUserName) throws Exception{
        String message = null;

        item it = new item();
        it.setTitle("马云,何许人也?");
        it.setDescription("阿里巴巴集团创始人");
        it.setPicUrl("http://2wh8ui.natappfree.cc/WinXin/wxSource/maYun.jpg");
        it.setUrl("https://baike.baidu.com/item/%E9%A9%AC%E4%BA%91/6252?fr=aladdin");

        item it2 = new item();
        it2.setTitle("马化腾,何许人也?");
        it2.setDescription("企业家");
        it2.setPicUrl("http://2wh8ui.natappfree.cc/WinXin/wxSource/maHuaTeng.jpg");
        it2.setUrl("https://baike.baidu.com/item/%E9%A9%AC%E5%8C%96%E8%85%BE/124988?fr=aladdin");

        List<item> Articles = new ArrayList<>();
        Articles.add(it);
        Articles.add(it2);


        articlesMessage articlesMessage = new articlesMessage();
        articlesMessage.setArticleCount(2);
        articlesMessage.setToUserName(fromUserName);
        articlesMessage.setFromUserName(toUserName);
        articlesMessage.setCreateTime(new Date().getTime());
        articlesMessage.setMsgType(userResp_message_type_news);
        articlesMessage.setArticles(Articles);

        message = articelsMessageToXml(articlesMessage);
        System.out.println(message);
        return message;
    }
  • 将图文信息拼装成xml格式
public static String articelsMessageToXml(articlesMessage articlesMessage){
        XStream stream = new XStream();//System.out.println(stream.toXML(text));//全类名<zk.entity.textMessage>
        stream.alias("xml",articlesMessage.getClass());//首尾全类名改成<xml>
        stream.alias("item",new item().getClass());//首尾全类名改成<xml>
        return stream.toXML(articlesMessage);
    }
3.测试回复图文效果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值