The replies to the WeChat public account development are for your reference. The specific content is as follows
Description of main parameters of graphic and text messages
Through the official WeChat message interface guide, you can see the parameters of graphic and text messages, as shown in the figure below:
From the above picture, we can learn:
1. The number of graphic messages is limited to 10, that is, the value of ArticleCount in the graphic messages (the number of graphic messages is limited to 10)
2. For graphic and text messages, the picture of the first graphic and text is displayed as a large picture, while the pictures of other graphic and text are displayed as a small picture.
3. The recommended size of the first picture is 640*320, and the recommended picture is 80*80 for other pictures
The following starts with implementation:
Base class for request message:
import com.thoughtworks.xstream.annotations.XStreamAlias;import java.io.Serializable;/** * @author inchlifc */public class BaseMessage implements Serializable { @XStreamAlias("ToUserName") @XStreamCDATA private String ToUserName; @XStreamAlias("FromUserName") @XStreamCDATA private String FromUserName; @XStreamAlias("CreateTime") private Long CreateTime; @XStreamAlias("MsgType") @XStreamCDATA private String MsgType; public BaseMessage() { super(); } public BaseMessage(String fromUserName, String toUserName) { super(); FromUserName = fromUserName; ToUserName = toUserName; CreateTime = System.currentTimeMillis(); } 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 Long getCreateTime() { return CreateTime; } public void setCreateTime(Long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; }}Graphic and text message category:
import com.thoughtworks.xstream.annotations.XStreamAlias;import java.util.List;@XStreamAlias("xml")public class ArticlesMessage extends BaseMessage { @XStreamAlias("ArticleCount") private int ArticleCount; @XStreamAlias("Articles") private List<ArticlesItem> Articles; public int getArticleCount() { return ArticleCount; } public void setArticleCount(int articleCount) { ArticleCount = articleCount; } public List<ArticlesItem> getArticles() { return Articles; } public void setArticles(List<ArticlesItem> articles) { Articles = articles; }}Articles class in the text and text messages:
import com.thoughtworks.xstream.annotations.XStreamAlias;import java.util.List;@XStreamAlias("Articles")public class Articles { private List<ArticlesItem> Articles;}ArticlesItem class in the text and text messages:
import com.thoughtworks.xstream.annotations.XStreamAlias;import java.io.Serializable;@XStreamAlias("item")public class ArticlesItem implements Serializable { @XStreamAlias("Title") @XStreamCDATA private String Title; @XStreamAlias("Description") @XStreamCDATA private String Description; @XStreamAlias("PicUrl") @XStreamCDATA private String PicUrl; @XStreamAlias("Url") @XStreamCDATA 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; }}Service layer implementation method:
Packaging method
/** * Get blog text messages* * @param customName * @param serverName * @param createTime * @return */ private ArticlesMessage getBlogMessage(String customName, String serverName, Long createTime) { ArticlesMessage outputMsg = new ArticlesMessage(); outputMsg.setFromUserName(serverName); outputMsg.setToUserName(custermName); outputMsg.setCreateTime(createTime); outputMsg.setMsgType(MsgType.NEWS.getValue()); List<ArticlesItem> articles = new ArrayList<>(); ArticlesItem item1 = new ArticlesItem(); item1.setTitle("Check to enter the evening cool breeze blog"); item1.setPicUrl(WechatConstant.BASE_SERVER + "resources/images/wechat/a.png"); item1.setUrl("https://my.oschina.net/inchlifc/blog"); articles.add(item1); outputMsg.setArticles(articles); outputMsg.setArticleCount(articles.size()); return outputMsg; }If you enter the number 1, return to the text message push
// Process the received message ServletInputStream in = request.getInputStream(); // Convert POST stream to XStream object XStream xs = new XStream(); xs = SerializeXmlUtil.createXstream(); XStream.setupDefaultSecurity(xs); xs.allowTypes(new Class[]{TextMessage.class, InputMessage.class, ArticlesMessage.class}); xs.processAnnotations(InputMessage.class); xs.processAnnotations(ArticlesMessage.class); xs.processAnnotations(ImageMessage.class); // Map the xml node data under the specified node into objects xs.alias("xml", InputMessage.class); // Convert streams to string StringBuilder xmlMsg = new StringBuilder(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1; ) { xmlMsg.append(new String(b, 0, n, "UTF-8")); } logger.info("Message received====" + xmlMsg.toString()); // Convert xml content to InputMessage object InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString()); // Server String servername = inputMsg.getToUserName(); // Client String customname = inputMsg.getFromUserName(); // Receive time long createTime = inputMsg.getCreateTime(); // Return time Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000; //Take the text content String content = inputMsg.getContent(); //Get the message type String msgType = inputMsg.getMsgType();if (MsgType.TEXT.getValue().equals(msgType)) { //Input 1 Push blog information if ("1".equals(content)) { logger.info("Text 1 received"); ArticlesMessage outputMsg = getBlogMessage(custermname, servername, returnTime); logger.info("Return to blog text message===" + xs.toXML(outputMsg)); response.getWriter().write(xs.toXML(outputMsg)); }}Running results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.