本篇文章给大家分享的是有关微信公众平台开发中使用Java如何实现一个消息回复功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。(一)回复文本消息在前面我们已经完成了对消息的分类
本篇文章给大家分享的是有关微信公众平台开发中使用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; public class MessageUtil { public static final String RESP_MESSAGE_TYPE_TEXT = "text"; public static final String RESP_MESSAGE_TYPE_MUSIC = "music"; public static final String RESP_MESSAGE_TYPE_NEWS = "news"; public static final String RESP_MESSAGE_TYPE_Image = "image"; public static final String RESP_MESSAGE_TYPE_Voice = "voice"; public static final String RESP_MESSAGE_TYPE_Video = "video"; 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_LINK = "link"; public static final String REQ_MESSAGE_TYPE_LOCATION = "location"; 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_EVENT = "event"; public static final String EVENT_TYPE_SUBSCRIBE = "subscribe"; public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe"; public static final String EVENT_TYPE_CLICK = "CLICK"; public static final String EVENT_TYPE_VIEW = "VIEW"; public static final String EVENT_TYPE_LOCATION = "LOCATION"; public static final String EVENT_TYPE_SCAN = "SCAN"; @SuppressWarnings("unchecked") public static Map<String, String> parseXml(HttpServletRequest request) throws Exception { // 将解析结果存储在HashMap中 Map<String, String> map = new HashMap<String, String>(); // 从request中取得输入流 InputStream inputStream = request.getInputStream(); // 读取输入流 SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); // 得到xml根元素 Element root = document.getRootElement(); // 得到根元素的所有子节点 List<Element> elementList = root.elements(); // 遍历所有子节点 for (Element e : elementList) map.put(e.getName(), e.getText()); // 释放资源 inputStream.close(); inputStream = null; return map; } public static String textMessageToXml(TextMessage textMessage) { xstream.alias("xml", textMessage.getClass()); return xstream.toXML(textMessage); } public static String newsMessageToXml(NewsMessage newsMessage) { xstream.alias("xml", newsMessage.getClass()); xstream.alias("item", new Article().getClass()); return xstream.toXML(newsMessage); } public static String imageMessageToXml(ImageMessage imageMessage) { xstream.alias("xml", imageMessage.getClass()); return xstream.toXML(imageMessage); } public static String voiceMessageToXml(VoiceMessage voiceMessage) { xstream.alias("xml", voiceMessage.getClass()); return xstream.toXML(voiceMessage); } public static String videoMessageToXml(VideoMessage videoMessage) { xstream.alias("xml", videoMessage.getClass()); return xstream.toXML(videoMessage); } public static String musicMessageToXml(MusicMessage musicMessage) { xstream.alias("xml", musicMessage.getClass()); return xstream.toXML(musicMessage); } private static XStream xstream = new XStream(new XppDriver() { public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { // 对所有xml节点的转换都增加CDATA标记 boolean cdata = true; @SuppressWarnings("rawtypes") public void startnode(String name, Class clazz) { super.startNode(name, clazz); } protected void writeText(QuickWriter writer, String text) { if (cdata) { writer.write("<![CDATA["); writer.write(text); writer.write("]]>"); } else { writer.write(text); } } }; } });}
--结束END--
本文标题: 微信公众平台开发中使用Java如何实现一个消息回复功能
本文链接: https://www.lsjlt.com/news/226791.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0