Java Development Micro-letter public number receive and passive reply ordinary message _java

Source: Internet
Author: User
Tags cdata documentation format definition

The article said how to connect the public number, this article on the micro-letter public number of the most basic functions: ordinary message reception and reply. When it comes to ordinary news, what is the ordinary message defined by the micro-credit Developer's documentation, which includes the following categories :

1. Text message
2. Picture message
3. Voice message
4. Video message
5. Small video message
6. Location information
7. Link message (passive reply message)

Common messages for passive replies include:

1. Reply to Text message
2. Reply to Picture message
3. Reply to Voice message
4. Reply to video message
5. Reply to Music message
6. Reply to Text message

In fact, receive messages and passive reply message These two actions are not separated, this is an interactive scene, the general situation is the public number through the analysis received the message, will give corresponding reply. Of course, some special business can not be ruled out.

How to receive messages

The XML format for the 7 messages that you want to receive is not listed here, please go to the official document to see the specific format definition and attribute description. The format is simple, and the basic common properties include Tousername, Fromusername, Createtime, Msgtype, MsgId, and each type has its own special attributes.

See here, in fact, it is clear that the process of receiving the message is to get the POST request this XML, and then analyze the XML process. The entrance to the POST request is still the address of the previously mentioned micro-public number access, and all requests for the public number will go to this entry, except that the access is a GET request and, in other cases, a POST request. Processing XML here is the Dom4j,xml processing code, which calls the Parsexml method in the servlet's post method:

 public static Map Parsexml (HttpServletRequest request) throws Exception {//To store the resolution results in has
Hmap map map = new HashMap ();
Obtain input stream InputStream InputStream = Request.getinputstream () from request; * * Read the body content of request This method causes the stream to read the problem premature end of file. Nested Exception: * Premature end of file String requestbody = * inputstream2string (InputStream);
System.out.println (Requestbody);
*//Read input stream Saxreader reader = new Saxreader ();
Document document = Reader.read (InputStream);
Gets the XML root element root = Document.getrootelement ();
Gets all the child nodes of the root element list<element> elementlist = root.elements ();
Iterate through all child nodes for (Element e:elementlist) map.put (E.getname (), E.gettext ());
Releasing resources inputstream.close ();
InputStream = null;
return map; private static String inputstream2string (InputStream is) throws IOException {Bytearrayoutputstream BAOs = new ByteArray
OutputStream ();
int i =-1;
while ((i = Is.read ())!=-1) {baos.write (i);} return baos.tostring (); }

How to reply to a message passively

I'm going to show you a message that constructs a reply based on this logic. Receive a text message "text", reply to a text message, receive "picture", reply to a picture message, receive "Voice", reply to a voice message, receive "video", reply to a video message, receive "music", reply to a music message, receive "text", Reply to a text message.

To reply to a text message as an explanation:

<xml>
<tousername><![ cdata[the person who sent the message, that subscriber]]></tousername>
<fromusername><![ cdata[public number itself]]></fromusername>
<CreateTime> message creation time (cosmetic) </CreateTime>
<msgtype ><! [cdata[text]]></msgtype>
<content><![ cdata[message content]]></content>
</xml>

The first two properties can be obtained from the received message in the form of the following message:

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername> 
<CreateTime>1348831860</CreateTime>
<msgtype ><! [cdata[text]]></msgtype>
<content><![ Cdata[this is a text]]></content>
<MsgId>1234567890123456</MsgId>

The tousername in the receiving message format is the fromusername of the reply message, and the fromusername in the receiving message format is the tousername of the reply message.

Createtime the time stamp sent for the message. Msgtype is the message type, and the literal is text. Content is the message contents.

The reply of each type of message is to construct the XML format content of this type, which is similar in format, but the XML content constructed by music, video, voice and graphic format is slightly more complicated than that of text message. Refer to the official documentation. Do not repeat here, I believe you can understand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.