這篇文章主要介紹了如何使用java開發(fā)微信公眾號之回復(fù)圖文消息功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、永新ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的永新網(wǎng)站制作公司
具體內(nèi)容如下
圖文消息的主要參數(shù)說明
通過微信官方的消息接口指南,可以看到對圖文消息的參數(shù)介紹,如下圖所示:
從上圖可以了解到:
1、圖文消息的個數(shù)限制為10,也就是圖文中ArticleCount的值(圖文消息的個數(shù),限制在10條以內(nèi))
2、對于圖文消息,第一條圖文的圖片顯示為大圖,其他圖文的圖片顯示為小圖。
3、第一條圖文的圖片大小建議為640*320,其他圖文的圖片建議為80*80
下面開始實現(xiàn):
請求消息的基類:
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; } }
圖文消息類:
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 ListArticles; public int getArticleCount() { return ArticleCount; } public void setArticleCount(int articleCount) { ArticleCount = articleCount; } public List getArticles() { return Articles; } public void setArticles(List articles) { Articles = articles; } }
圖文消息中的Articles類:
import com.thoughtworks.xstream.annotations.XStreamAlias; import java.util.List; @XStreamAlias("Articles") public class Articles { private ListArticles; }
圖文消息中的ArticlesItem類:
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層實現(xiàn)方法:
封裝方法
/** * 獲取博客圖文消息 * * @param custermName * @param serverName * @param createTime * @return */ private ArticlesMessage getBlogMessage(String custermName, String serverName, Long createTime) { ArticlesMessage outputMsg = new ArticlesMessage(); outputMsg.setFromUserName(serverName); outputMsg.setToUserName(custermName); outputMsg.setCreateTime(createTime); outputMsg.setMsgType(MsgType.NEWS.getValue()); Listarticles = new ArrayList<>(); ArticlesItem item1 = new ArticlesItem(); item1.setTitle("晚天吹涼風(fēng)"); item1.setDescription("點擊進入晚天吹涼風(fēng)博客"); 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; }
判斷如果輸入數(shù)字1,返回圖文消息推送
// 處理接收消息 ServletInputStream in = request.getInputStream(); // 將POST流轉(zhuǎn)換為XStream對象 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); // 將指定節(jié)點下的xml節(jié)點數(shù)據(jù)映射為對象 xs.alias("xml", InputMessage.class); // 將流轉(zhuǎn)換為字符串 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("收到消息====" + xmlMsg.toString()); // 將xml內(nèi)容轉(zhuǎn)換為InputMessage對象 InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString()); // 服務(wù)端 String servername = inputMsg.getToUserName(); // 客戶端 String custermname = inputMsg.getFromUserName(); // 接收時間 long createTime = inputMsg.getCreateTime(); // 返回時間 Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000; //接手文本內(nèi)容 String content = inputMsg.getContent(); // 取得消息類型 String msgType = inputMsg.getMsgType(); if (MsgType.TEXT.getValue().equals(msgType)) { //輸入1 推送博客信息 if ("1".equals(content)) { logger.info("收到文本1"); ArticlesMessage outputMsg = getBlogMessage(custermname, servername, returnTime); logger.info("返回博客圖文消息===" + xs.toXML(outputMsg)); response.getWriter().write(xs.toXML(outputMsg)); } }
運行結(jié)果:
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用java開發(fā)微信公眾號之回復(fù)圖文消息功能”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!