微信公众号开发之回复图文消息java代码

时间:2021-05-02

微信公众号开发之回复图文消息,供大家参考,具体内容如下

图文消息的主要参数说明

通过微信官方的消息接口指南,可以看到对图文消息的参数介绍,如下图所示:

从上图可以了解到:

1、图文消息的个数限制为10,也就是图文中articlecount的值(图文消息的个数,限制在10条以内)

2、对于图文消息,第一条图文的图片显示为大图,其他图文的图片显示为小图。

3、第一条图文的图片大小建议为640*320,其他图文的图片建议为80*80

下面开始实现:

请求消息的基类:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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; } }

图文消息类:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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类:

? 1 2 3 4 5 6 7 import com.thoughtworks.xstream.annotations.xstreamalias; import java.util.list; @xstreamalias("articles") public class articles { private list<articlesitem> articles; }

图文消息中的articlesitem类:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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层实现方法:

封装方法

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /** * 获取博客图文消息 * * @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()); list<articlesitem> articles = new arraylist<>(); articlesitem item1 = new articlesitem(); item1.settitle("晚天吹凉风"); item1.setdescription("点击进入晚天吹凉风博客"); 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; }

判断如果输入数字1,返回图文消息推送

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 // 处理接收消息 servletinputstream in = request.getinputstream(); // 将post流转换为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); // 将指定节点下的xml节点数据映射为对象 xs.alias("xml", inputmessage.class); // 将流转换为字符串 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内容转换为inputmessage对象 inputmessage inputmsg = (inputmessage) xs.fromxml(xmlmsg.tostring()); // 服务端 string servername = inputmsg.gettousername(); // 客户端 string custermname = inputmsg.getfromusername(); // 接收时间 long createtime = inputmsg.getcreatetime(); // 返回时间 long returntime = calendar.getinstance().gettimeinmillis() / 1000; //接手文本内容 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)); } }

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/PreachChen/archive/2018/03/27/8655394.html

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章