1.首先分析图文的数据结构
![列表内容](https://img-blog.csdn.net/20170906200726481?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRWRpc29uXzAz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<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
}
public static String articelsMessageToXml(articlesMessage articlesMessage){
XStream stream = new XStream();
stream.alias("xml",articlesMessage.getClass());
stream.alias("item",new item().getClass());
return stream.toXML(articlesMessage);
}
3.测试回复图文效果
![这里写图片描述](https://img-blog.csdn.net/20170906202109441?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRWRpc29uXzAz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)