本例中有些引用的类与方法不做过多介绍,之后会提供完整源码下载,请自行查看。
本篇根据开发者文档-接收普通消息编写。请对照查看,一些传入与返回参数就不过多介绍。地址为:https://mp.weixin.qq.com/wiki/14/f79bdec63116f376113937e173652ba2.html
当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。请注意:
- 关于重试的消息排重,推荐使用msgid排重。
- 微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次。假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试。详情请见“发送消息-被动回复消息”。
- 为了保证更高的安全保障,开发者可以在公众平台官网的开发者中心处设置消息加密。开启加密后,用户发来的消息会被加密,公众号被动回复用户的消息也需要加密(但开发者通过客服接口等API调用形式向用户发送消息,则不受影响)。
消息类型有:
public static final String REQ_MESSAGE_TYPE_TEXT = "text"; //请求消息类型:文本 public static final String REQ_MESSAGE_TYPE_IMAGE = "image"; //请求消息类型:图片 public static final String REQ_MESSAGE_TYPE_VOICE = "voice"; //请求消息类型:语音 public static final String REQ_MESSAGE_TYPE_VIDEO = "video"; //请求消息类型:视频 public static final String REQ_MESSAGE_TYPE_SHORTVIDEO = "shortvideo"; //请求消息类型:小视频 public static final String REQ_MESSAGE_TYPE_LINK = "link"; //请求消息类型:链接 public static final String REQ_MESSAGE_TYPE_LOCATION = "location"; //请求消息类型:地理位置 public static final String REQ_MESSAGE_TYPE_EVENT = "event"; //请求消息类型:推送
/* * 接收普通消息 * http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html */ @XmlRootElement public class BaseReqMsg { @XmlElement(name = "ToUserName") private String ToUserName; // 开发者微信号 @XmlElement(name = "FromUserName") private String FromUserName; // 发送方帐号(一个OpenID) @XmlElement(name = "CreateTime") private long CreateTime; // 消息创建时间 (整型) @XmlElement(name = "MsgType") private String MsgType; // 消息类型(text/image/location/link) @XmlElement(name = "MsgId") private long MsgId; // 消息id,64位整型 }
文本消息
/*
* MsgType--->
* text:文本消息
*/
@XmlRootElement
public class TextReqMsg extends BaseReqMsg {
@XmlElement(name = "Content")
private String Content; //文本消息内容
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1348831860</CreateTime>
* <MsgType><![CDATA[text]]></MsgType>
* <Content><![CDATA[this is a test]]></Content>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static TextReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "textReqMsg");
JAXBContext context = JAXBContext.newInstance(TextReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (TextReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}
图片消息
/*
* MsgType--->
* image:图片消息
*/
@XmlRootElement
public class ImageReqMsg extends BaseReqMsg {
@XmlElement(name = "PicUrl")
private String PicUrl ; //图片链接(由系统生成)
@XmlElement(name = "MediaId")
private String MediaId ; // 图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1348831860</CreateTime>
* <MsgType><![CDATA[image]]></MsgType>
* <PicUrl><![CDATA[this is a url]]></PicUrl>
* <MediaId><![CDATA[media_id]]></MediaId>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static ImageReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "imageReqMsg");
JAXBContext context = JAXBContext.newInstance(ImageReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (ImageReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}
语音消息
/*
* MsgType--->
* voice:语音消息
*/
@XmlRootElement
public class VoiceReqMsg extends BaseReqMsg {
@XmlElement(name = "MediaId")
private String MediaId; //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
@XmlElement(name = "Format")
private String Format; // 语音格式:amr
@XmlElement(name = "Recognition")
private String Recognition; // 语音识别结果,UTF8编码.需开通开通语音识别
public String getMediaID() {
return MediaId;
}
public void setMediaID(String mediaID) {
MediaId = mediaID;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public String getRecognition() {
return Recognition;
}
public void setRecognition(String recognition) {
Recognition = recognition;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1357290913</CreateTime>
* <MsgType><![CDATA[voice]]></MsgType>
* <MediaId><![CDATA[media_id]]></MediaId>
* <Format><![CDATA[Format]]></Format>
* <Recognition><![CDATA[腾讯微信团队]]></Recognition>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static VoiceReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "voiceReqMsg");
JAXBContext context = JAXBContext.newInstance(VoiceReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (VoiceReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}
视频消息/小视频消息
/*
* MsgType--->
* shortvideo:小视频消息
* video:视频消息
*/
@XmlRootElement
public class VideoReqMsg extends BaseReqMsg {
@XmlElement(name = "MediaId")
private String MediaId; //视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
@XmlElement(name = "ThumbMediaId")
private String ThumbMediaId;//视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getThumbMediaId() {
return ThumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
ThumbMediaId = thumbMediaId;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1357290913</CreateTime>
* <MsgType><![CDATA[video|shortvideo ]]></MsgType>
* <MediaId><![CDATA[media_id]]></MediaId>
* <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static VideoReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "videoReqMsg");
JAXBContext context = JAXBContext.newInstance(VideoReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (VideoReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}
地理位置消息
/*
* MsgType--->
* location:地理位置消息
*/
@XmlRootElement
public class LocationReqMsg extends BaseReqMsg {
@XmlElement(name = "Location_X")
private float Location_X; //地理位置维度
@XmlElement(name = "Location_Y")
private float Location_Y; //地理位置经度
@XmlElement(name = "Scale")
private float Scale; //地图缩放大小
@XmlElement(name = "Label")
private String Label; //地理位置信息
public float getLocation_X() {
return Location_X;
}
public void setLocation_X(float location_X) {
Location_X = location_X;
}
public float getLocation_Y() {
return Location_Y;
}
public void setLocation_Y(float location_Y) {
Location_Y = location_Y;
}
public float getScale() {
return Scale;
}
public void setScale(float scale) {
Scale = scale;
}
public String getLabel() {
return Label;
}
public void setLabel(String label) {
Label = label;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1351776360</CreateTime>
* <MsgType><![CDATA[location]]></MsgType>
* <Location_X>23.134521</Location_X>
* <Location_Y>113.358803</Location_Y>
* <Scale>20</Scale>
* <Label><![CDATA[位置信息]]></Label>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static LocationReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "locationReqMsg");
JAXBContext context = JAXBContext.newInstance(LocationReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (LocationReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}
链接消息
/*
* MsgType--->
* link:链接消息
*/
@XmlRootElement
public class LinkReqMsg extends BaseReqMsg {
@XmlElement(name = "Title")
private String Title; //消息标题
@XmlElement(name = "Description")
private String Description; //消息描述
@XmlElement(name = "Url")
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 getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
/*
* 接收到的文本示例
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1351776360</CreateTime>
* <MsgType><![CDATA[link]]></MsgType>
* <Title><![CDATA[公众平台官网链接]]></Title>
* <Description><![CDATA[公众平台官网链接]]></Description>
* <Url><![CDATA[url]]></Url>
* <MsgId>1234567890123456</MsgId>
* </xml>
*/
public static LinkReqMsg requestMessage(String xmlStr) throws JAXBException {
xmlStr = xmlStr.replace("xml", "linkReqMsg");
JAXBContext context = JAXBContext.newInstance(LinkReqMsg.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (LinkReqMsg)unmarshaller.unmarshal(new StringReader(xmlStr));
}
}