消息"/>
java 微信图文消息
微信公众号开发之回复图文消息,供大家参考,具体内容如下
图文消息的主要参数说明
通过微信官方的消息接口指南,可以看到对图文消息的参数介绍,如下图所示:
从上图可以了解到:
1、图文消息的个数限制为10,也就是图文中articlecount的值(图文消息的个数,限制在10条以内)
2、对于图文消息,第一条图文的图片显示为大图,其他图文的图片显示为小图。
3、第一条图文的图片大小建议为640*320,其他图文的图片建议为80*80
下面开始实现:
请求消息的基类:
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 list articles;
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 list articles;
}
图文消息中的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层实现方法:
封装方法
/**
* 获取博客图文消息
*
* @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 articles = new arraylist<>();
articlesitem item1 = new articlesitem();
item1.settitle("晚天吹凉风");
item1.setdescription("点击进入晚天吹凉风博客");
item1.setpicurl(wechatconstant.base_server + "resources/images/wechat/a.png");
item1.seturl("");
articles.add(item1);
outputmsg.setarticles(articles);
outputmsg.setarticlecount(articles.size());
return outputmsg;
}
判断如果输入数字1,返回图文消息推送
// 处理接收消息
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));
}
}
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
更多推荐
java 微信图文消息
发布评论