微信公众平台开发中使用Java如何实现一个消息回复功能
发布时间:2020-11-17 16:11:11
来源:亿速云
阅读:82
作者:Leah
本篇文章给大家分享的是有关微信公众平台开发中使用Java如何实现一个消息回复功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
(一)回复文本消息
在前面我们已经完成了对消息的分类和回复消息实体的建立,这里回复文本消息需要用到的就是我们的TextMessage,我们把回复文本消息在【文本消息】类型中给出回复!在我们做消息回复的时候需要设置消息的接收人ToUserName(openid)、消息的发送方FromUserName、消息类型MsgType、创建时间CreateTime以及消息体Content,由于我们我们的消息回复格式是需要为xml,所以最终我们需要将其装换成xml再做返回输出!
首先我们在工具类MessageUtil的代码做出部分修改和添加,实现最后版本为:
package com.cuiyongzhi.wechat.util;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.D ocument;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.cuiyongzhi.wechat.message.resp.Article;
import com.cuiyongzhi.wechat.message.resp.ImageMessage;
import com.cuiyongzhi.wechat.message.resp.MusicMessage;
import com.cuiyongzhi.wechat.message.resp.NewsMessage;
import com.cuiyongzhi.wechat.message.resp.TextMessage;
import com.cuiyongzhi.wechat.message.resp.VideoMessage;
import com.cuiyongzhi.wechat.message.resp.VoiceMessage;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
/**
* ClassName: MessageUtil
*
* @Description: 消息工具类
* @author dapengniao
* @date 2016年3月7日 上午10:05:04
*/
public class MessageUtil {
/**
* 返回消息类型:文本
*/
public static final String RESP_MESSAGE_TYPE_TEXT = "text";
/**
* 返回消息类型:音乐
*/
public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
/**
* 返回消息类型:图文
*/

最低0.47元/天 解锁文章
5414

被折叠的 条评论
为什么被折叠?



