微信公众号开发 - 4.发送图文消息
前言
来看看图文消息的参数
XML实例,分析一下大概就是下面这个样子
<xml>
<!--图文消息-->
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<!--图文消息的内容-->
<Articles>
<!--第一条-->
<item>
<Title><![CDATA[title1]]></Title>
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<!--第二条-->
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml>
实体
将重复使用的类提取为父类
package cn.notemi.po;
/**
* Title:BaseMessage
* Description:message 重复的属性提取为父类
*
* @author Flicker
* @create 2017-07-26 下午 4:34
**/
public class BaseMessage {
private String ToUserName;
private String FromUserName;
private String CreateTime;
private String MsgType;
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public String getCreateTime() {
return CreateTime;
}
public void setCreateTime(String createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
}
图文消息
package cn.notemi.po;
import java.util.List;
/**
* Title:NewsMessage
* Description:图文消息
*
* @author Flicker
* @create 2017-07-26 下午 4:34
**/
public class NewsMessage extends BaseMessage{
private int ArticleCount;
private List<News> Articles;
public int getArticleCount() {
return ArticleCount;
}
public void setArticleCount(int articleCount) {
ArticleCount = articleCount;
}
public List<News> getArticles() {
return Articles;
}
public void setArticles(List<News> articles) {
Articles = articles;
}
}
图文消息内容
package cn.notemi.po;
/**
* Title:News
* Description:图文消息内容
*
* @author Flicker
* @create 2017-07-26 下午 4:31
**/
public class News {
private String Title;
private String Description;
private String PicUrl;
private String Url;
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
}
之前的文字消息
package cn.notemi.po;
/**
* Title:TextMessage
* Description:文字消息
*
* @author Flicker
* @create 2017-07-25 下午 8:40
**/
public class TextMessage extends BaseMessage{
private String Content;
private String MsgId;
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
}
转化拼接图文消息
package cn.notemi.util;
import cn.notemi.po.News;
import cn.notemi.po.NewsMessage;
import cn.notemi.po.TextMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.*;
/**
* Title:MessageUtil
* Description:消息的转换
*
* @author Flicker
* @create 2017-07-25 下午 8:19
**/
public class MessageUtil {
public static final String MESSAGE_NEWS = "news";
/**
* 将图文消息消息对象转为xml
* @param newsMessage
* @return
*/
public static String NewsMessageToXml(NewsMessage newsMessage){
XStream xStream = new XStream();
xStream.alias("xml",newsMessage.getClass());
xStream.alias("item",new News().getClass());
return xStream.toXML(newsMessage);
}
/**
* 拼装图文消息
* @param toUserName
* @param fromUserName
* @return
*/
public static String initNewsMessage(String toUserName,String fromUserName){
String message = null;
List<News> newsList = new ArrayList<News>();
NewsMessage newsMessage = new NewsMessage();
//内容
News news = new News();
news.setTitle("小米笔记介绍");
news.setDescription("这里是小米笔记的介绍介绍介绍");
news.setPicUrl("http://6hsfsi.natappfree.cc/static/img/logo.png");
news.setUrl("http://notemi.cn");
newsList.add(news);
//图文消息属性
newsMessage.setToUserName(fromUserName);
newsMessage.setFromUserName(toUserName);
newsMessage.setCreateTime(String.valueOf(new Date().getTime()));
newsMessage.setMsgType(MESSAGE_NEWS);
newsMessage.setArticles(newsList);
newsMessage.setArticleCount(newsList.size());
message = NewsMessageToXml(newsMessage);
return message;
}
}
Controller
if ("1".equals(content)){
message = MessageUtil.initNewsMessage(toUserName,fromUserName);
}
测试
源代码
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »