Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate various types of messages on Wechat by Java

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how Java encapsulates various types of messages on Wechat. What is introduced in this article is very detailed and has certain reference value. Interested friends must finish reading it!

Step 1: find the com.wtz.message.response package and create a new class Image.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 8:38:37 * version:1.0 * description: entity class of picture * / public class Image {/ / picture media ID private String MediaId; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;}}

Step 2: find the com.wtz.message.response package and create a new class ImageMessage.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 8:49:57 * version:1.0 * description: picture message class * / public class ImageMessage extends BaseMessage {/ / picture object private Image image; public Image getImage () {return image;} public void setImage (Image image) {this.image = image;}}

Step 3: find the com.wtz.message.response package and create a new class Video.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:02:14 * version:1.0 * description: entity class of video * / public class Video {/ / Media ID private String MediaId; / / title of video message private String Title; / / description of video message private String Description; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId } public String getTitle () {return Title;} public void setTitle (String title) {Title = title;} public String getDescription () {return Description;} public void setDescription (String description) {Description = description;}}

Step 4: find the com.wtz.message.response package and create a new class VideoMessage.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:10:14 * version:1.0 * description: video message class * / public class VideoMessage extends BaseMessage {/ / Video object private Video video; public Video getVideo () {return video;} public void setVideo (Video video) {this.video = video;}}

Step 5: find the com.wtz.message.response package and create a new class Voice.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:21:47 * version:1.0 * description: voice message entity Class * / public class Voice {/ / Voice Media number private String MediaId; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;}}

Step 6: find the com.wtz.message.response package and create a new class VoiceMessage.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:44:58 * version:1.0 on April 20, 2017

* description: voice message class * / public class VoiceMessage extends BaseMessage {/ / Voice object private Voice voice; public Voice getVoice () {return voice;} public void setVoice (Voice voice) {this.voice = voice;}}

Step 7: find the com.wtz.message.response package and create a new class Music.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:48:11 * version:1.0 * description: music message entity class * / public class Music {/ / music title private String Title; / / music description private String Description; / / music link private String MusicUrl; / / media ID private String ThumbMediaId of high quality music link private String HQMusicUrl; / / thumbnail 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 getMusicUrl () {return MusicUrl;} public void setMusicUrl (String musicUrl) {MusicUrl = musicUrl;} public String getHQMusicUrl () {return HQMusicUrl;} public void setHQMusicUrl (String hQMusicUrl) {HQMusicUrl = hQMusicUrl;} public String getThumbMediaId () {return ThumbMediaId } public void setThumbMediaId (String thumbMediaId) {ThumbMediaId = thumbMediaId;}}

Step 8: find the com.wtz.message.response package and create a new class MusicMessage.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 9:58:21 on 20 April 2017 *

Version:1.0

*

Description: music message class

* / public class MusicMessage extends BaseMessage {/ / Music object private Music music; public Music getMusic () {return music;} public void setMusic (Music music) {this.music = music;}}

Step 9: find the com.wtz.message.response package and create a new class Article.java

Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date 10:05:29 on 20 April 2017 *

Version:1.0

*

Description: picture and text entity class

* / public class Article {/ / Picture-text title private String Title; / / Picture-text description private String Description; / / Picture Link, which supports JPG,PNG format. Better effect is large picture private String PicUrl; / / Picture-text message Jump Link 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;}}

Step 10: find the com.wtz.message.response package and create a new class NewsMessage.java

Package com.wtz.message.response;import java.util.List;/** * @ author wangtianze QQ:864620012 * @ date 10:16:22 on 20 April 2017 *

Version:1.0

*

Description: picture and text message class

* / public class NewsMessage extends BaseMessage {/ / number of picture and text private int ArticleCount; / / multiple picture and text messages. By default, the first item is large image 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;}}.

Step 11: create a new package com.wtz.message.request and a new class BaseMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 10:53:23 on 20 April 2017 *

Version:1.0

*

Description: request message base class (ordinary user-> public account)

*

The perspective of standing on the server side: receiving information

* / public class BaseMessage {/ / developer Wechat account private String ToUserName; / / sender account (an OpenId) private String FromUserName; / / message creation time (integer) private long CreateTime; / / message type private String MsgType; / / message ID,64 bit integer private long MsgId; 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;} public long getMsgId () {return MsgId;} public void setMsgId (long msgId) {MsgId = msgId;}}

Step 12: find the com.wtz.message.request package and create a new class TextMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 11:04:35 on 20 April 2017 *

Version:1.0

*

Description: text messa

* / public class TextMessage extends BaseMessage {/ / message content private String Content; public String getContent () {return Content;} public void setContent (String content) {Content = content;}}

Step 13: find the com.wtz.message.request package and create a new class ImageMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 11:20:50 on 20 April 2017 *

Version:1.0

*

Description: picture message

* / public class ImageMessage extends BaseMessage {/ / Picture Link private String PicUrl; / / Picture message Media ID private String MediaId; public String getPicUrl () {return PicUrl;} public void setPicUrl (String picUrl) {PicUrl = picUrl;} public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;}}

Step 14: find the com.wtz.message.request package and create a new class VoiceMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 11:27:40 on 20 April 2017 *

Version:1.0

*

Description: voice message class

* / public class VoiceMessage extends BaseMessage {/ / Media ID private String MediaId; / / Voice format private String Format; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;} public String getFormat () {return Format;} public void setFormat (String format) {Format = format;}}

Step 15: find the com.wtz.message.request package and create a new class VideoMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:28:03 p.m. *

Version:1.0

*

Description: video message class

* / public class VideoMessage extends BaseMessage {/ / Video message media ID private String MediaId; / / Media ID private String ThumbMediaId; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;} public String getThumbMediaId () {return ThumbMediaId;} public void setThumbMediaId (String thumbMediaId) {ThumbMediaId = thumbMediaId;}}

Step 16: find the com.wtz.message.request package and create a new class LocatonMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:27:40 p.m. *

Version:1.0

*

Description: geolocation message class

* / public class LocationMessage extends BaseMessage {/ / Latitude private String Location_X; / / Geographic location Longitude private String Location_Y; / / Map Zoom size private String Scale; / / Geographic location Information private String Label; public String getLocation_X () {return Location_X;} public void setLocation_X (String location_X) {Location_X = location_X;} public String getLocation_Y () {return Location_Y } public void setLocation_Y (String location_Y) {Location_Y = location_Y;} public String getScale () {return Scale;} public void setScale (String scale) {Scale = scale;} public String getLabel () {return Label;} public void setLabel (String label) {Label = label;}}

Step 17: find the com.wtz.message.request package and create a new class LinkMessage.java

Package com.wtz.message.request;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 1:58:04 p.m. *

Version:1.0

*

Description: link message class

* / public class LinkMessage extends BaseMessage {/ / message title private String Title; / / message description private String Description; / / message link 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;}}

Step 18: create a new package com.wtz.message.event and a new class BaseEventMessage.java

Package com.wtz.message.event;/** * @ author wangtianze QQ:864620012 * @ date 10:44:09 on 20 April 2017 *

Version:1.0

*

Description: basic event message class

* / public class BaseEventMessage {/ / developer Wechat account private String ToUserName; / / sender account (an OpenID) private String FromUserName; / / message creation time (integer) private long CreateTime; / / message type private String MsgType; / / event type private String Event; 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;} public String getEvent () {return Event;} public void setEvent (String event) {Event = event;}}

Step 19: find the package com.wtz.message.event and create a new class QRCodeEventMessage.java

Package com.wtz.message.event;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:24:30 p.m. *

Version:1.0

*

Description: scan the event class of QR code with parameters

* / public class QRCodeEventMessage extends BaseEventMessage {/ / event key value private String EventKey; / / is used to exchange the QR code picture private String Ticket; public String getEventKey () {return EventKey;} public void setEventKey (String eventKey) {EventKey = eventKey;} public String getTicket () {return Ticket;} public void setTicket (String ticket) {Ticket = ticket;}}

Step 20: find the package com.wtz.message.event and create a new class SubscribeEventMessage.java

Package com.wtz.message.event;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:27:11 p.m. *

Version:1.0

*

Description: follow / unfollow event class

* / public class SubscribeEventMessage extends BaseEventMessage {}

Step 21: find the package com.wtz.message.event and create a new class MenuEventMessage.java

Package com.wtz.message.event;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:32:42 p.m. *

Version:1.0

*

Description: custom menu event class

* / public class MenuEventMessage extends BaseEventMessage {/ / event key value, corresponding to private String EventKey; public String getEventKey () {return EventKey;} public void setEventKey (String eventKey) {EventKey = eventKey;}}

Step 22: find the package com.wtz.message.event and create a new class LocationEventMessage.java

Package com.wtz.message.event;/** * @ author wangtianze QQ:864620012 * @ date 20 April 2017 3:37:50 p.m. *

Version:1.0

*

Description: escalate geolocation event class

* / public class LocationEventMessage extends BaseEventMessage {/ / Geographic location latitude private String Latitude; / / Geographic location longitude private String Longitude; / / Geographic location accuracy private String Precision; public String getLatitude () {return Latitude;} public void setLatitude (String latitude) {Latitude = latitude;} public String getLongitude () {return Longitude;} public void setLongitude (String longitude) {Longitude = longitude;} public String getPrecision () {return Precision;} public void setPrecision (String precision) {Precision = precision;}}

Step 23: find the MessageUtil.java class under the package com.wtz.util, modify the class to add various message types and event types for requests and replies, and add methods to convert various types of messages into XML format

Package com.wtz.util;import java.io.IOException;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.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;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;import com.wtz.message.response.ImageMessage;import com.wtz.message.response.MusicMessage;import com.wtz.message.response.NewsMessage;import com.wtz.message.response.TextMessage;import com.wtz.message.response.VideoMessage;import com.wtz.message.response.VoiceMessage;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 3:29:58 *

Version:1.0

*

Description: message processing tool class

* / public class MessageUtil {/ / request message type: text public static final String REQ_MESSAGE_TYPE_TEXT = "text"; / / request message type: picture public static final String REQ_MESSAGE_TYPE_IMAGE = "image"; / / request message type: voice public static final String REQ_MESSAGE_TYPE_VOICE = "voice"; / / request message type: video public static final String REQ_MESSAGE_TYPE_VIDEO = "video" / / request message type: geographical location public static final String REQ_MESSAGE_TYPE_LOCATION = "location"; / / request message type: link public static final String REQ_MESSAGE_TYPE_LINK = "link"; / / request message type: event push public static final String REQ_MESSAGE_TYPE_EVENT = "event" / / event type: subscribe (subscription) public static final String EVENT_TYPE_SUBSCRIBE = "subscribe"; / / event type: unsubscribe (unsubscribe) public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe" / / event type: scan (scan QR code with parameters when the user has followed it) public static final String EVENT_TYPE_SCAN = "scan"; / / event type: LOCATION (reported geographic location) public static final String EVENT_TYPE_LOCATION = "LOCATION"; / / event type: CLICK (custom menu) public static final String EVENT_TYPE_CLICK = "CLICK" / /-- / response message type: text public static final String RESP_MESSAGE_TYPE_TEXT = "text"; / / response detail type: picture public static final String RESP_MESSAGE_TYPE_IMAGE = "image" / / response message type: voice public static final String RESP_MESSAGE_TYPE_VOICE = "voice"; / / response message type: video public static final String RESP_MESSAGE_TYPE_VIDEO = "video"; / / response detail type: music public static final String RESP_MESSAGE_TYPE_MUSIC = "music"; / / response message type: public static final String RESP_MESSAGE_TYPE_NEWS = "news" / / parse the content of each node public static Map parseXml (HttpServletRequest request) throws IOException {Map map = new HashMap () from the stream; / / get the stream object InputStream in = request.getInputStream () from the input stream / / build the SAX reader object SAXReader reader = new SAXReader (); try {/ / get the document object Document doc = reader.read (in) from the stream; / / get the root node Element root = doc.getRootElement (); / / get all child nodes under the root node List children = root.elements () For (Element e:children) {/ / traverses each node and puts map.put (e.getName (), e.getText ()) in map according to node name-node value; System.out.println ("the message XML sent by the user parses to:" + e.getName () + e.getText ());} / / closes stream in.close (); in = null } catch (DocumentException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return map;} / * * is used to expand node data according to, with CDATA segment * / private static XStream xstream = new XStream (new XppDriver () {public HierarchicalStreamWriter createWriter (Writer out) {return new PrettyPrintWriter (out) {boolean cdata = true; public void startNode (String name,Class clazz) {super.startNode (name,clazz)) } protected void writeText (QuickWriter writer,String text) {if (cdata) {writer.write (");} else {writer.write (text);};}}); / * * convert text message objects into XML format * params:textMessage text message objects * return:xml * / public static String messageToXml (TextMessage textMessage) {xstream.alias (" xml ", textMessage.getClass ()); return xstream.toXML (textMessage) } / * convert picture message objects to XML format * params:imageMessage * return:xml * / public static String messageToXml (ImageMessage imageMessage) {xstream.alias ("xml", imageMessage.getClass ()); return xstream.toXML (imageMessage);} / * convert voice message objects to XML format * params:voiceMessage * return:xml * / public static String messageToXml (VoiceMessage voiceMessage) {xstream.alias ("xml", voiceMessage.getClass ()); return xstream.toXML (voiceMessage) } / * convert video message object to XML format * params:videoMessage * return:xml * / public static String messageToXml (VideoMessage videoMessage) {xstream.alias ("xml", videoMessage.getClass ()); return xstream.toXML (videoMessage);} / * convert music message object to XML format * params:musicMessage * return:xml * / public static String messageToXml (MusicMessage musicMessage) {xstream.alias ("xml", musicMessage.getClass ()); return xstream.toXML (musicMessage) } / * convert Teletext message objects to XML format * params:newsMessage * return:xml * / public static String messageToXml (NewsMessage newsMessage) {xstream.alias ("xml", newsMessage.getClass ()); return xstream.toXML (newsMessage);}}

Step 24: find the ProcessService.java class under the package com.wtz.util and modify the dealRequest method to respond to various types of messages

Package com.wtz.util;import java.io.IOException;import java.util.Date;import java.util.Map;import javax.servlet.http.HttpServletRequest;import com.wtz.message.response.TextMessage;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 8:04:14 *

Version:1.0

*

Description: core service class

* / public class ProcessService {public static String dealRequest (HttpServletRequest request) {/ / message data in XML format String respXml = ""; / / text message content returned by default String respContent = "unknown message type"; / / call parseXml method to parse request message Map requestMap; try {requestMap = MessageUtil.parseXml (request); / / sender account String fromUserName = requestMap.get ("FromUserName"); / / developer Wechat String toUserName = requestMap.get ("ToUserName") / / message type String MsgType = requestMap.get ("MsgType"); / / reply text message TextMessage textMessage = new TextMessage (); textMessage.setToUserName (fromUserName); textMessage.setFromUserName (toUserName); textMessage.setCreateTime (new Date (). GetTime ()); textMessage.setMsgType (MessageUtil.RESP_MESSAGE_TYPE_TEXT); if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {respContent = "you sent a text message" } else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {respContent = "you sent a picture message";} else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {respContent = "you sent a voice message";} else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {respContent = "you sent a video message" } else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {respContent = "you sent a geolocation message";} else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_LINK)) {respContent = "you sent a link message";} else if (MsgType.equals (MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {/ / event type String eventType = requestMap.get ("Event") If (eventType.equals (MessageUtil.EVENT_TYPE_SUBSCRIBE)) {respContent = "Thank you for your attention" } else if (eventType.equals (MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {/ / TODO after unsubscribing, users will no longer receive messages sent by official account Therefore, there is no need to reply} else if (eventType.equals (MessageUtil.EVENT_TYPE_SCAN)) {/ / TODO process scan QR code event} else if (eventType.equals (MessageUtil.EVENT_TYPE_LOCATION)) {/ / TODO process report geolocation event} else if (eventType.equals (MessageUtil.EVENT_TYPE_CLICK)) {/ / TODO processing menu Click event}} / / set the text The content of the message textMessage.setContent (respContent) / / convert the text message object to xml respXml = MessageUtil.messageToXml (textMessage);} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return respXml;}}

Various types of messages are encapsulated.

The above is all the contents of this article entitled "how Java encapsulates various types of messages on Wechat". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report