Java wechat development API step 2: get and reply to messages
How to get and reply to messages through wechat development API is introduced below
1、 Description * this example is based on wechat development documents: http://mp.weixin.qq.com/wiki/home/index.html The latest version (4 / 3 / 2016 5:34:36 PM) for development demonstration* Editing platform: MyEclipse 10 7+win32+jdk1. 7+tomcat7. 0 * server: alicloud Windows Server 2008 64bits * platform requirements: servlet uses annotation method, platform requirements: j2ee6 0+、jdk6. 0+、tomcat7. 0 + * the demo focuses more on API parsing* For the convenience of test description, each test case is independent and does not depend on other methods. No more consideration is given to packaging* The demonstration shall be conducted according to the API requirements as far as possible. The purpose is to understand the use of documents and draw inferences from one example* Knowledge requirements: solid Java foundation, understanding of HTTP network communication knowledge, sufficient understanding of Java Web, JSON parsing * the demonstration source code of this part will be given at the end of each article. After analyzing the API, all demo source code will be given in the form of source code package* Current time: 4 / 3 / 2016 5:32:57 PM, subject to this time.
2、 Original document - Message Management (summary) • document address: http://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html Message management, receiving message - receiving common messages, receiving messages - receiving events, pushing messages, sending messages - passive reply messages, sending messages - sending and receiving messages in passive reply - customer service message, sending messages - sending messages, sending messages - template message interface, sending messages - template message operation specification, getting official account number automatic reply configuration.
3、 Document understanding • receiving messages ◦ the document explains this: when ordinary wechat users send messages to public accounts, the wechat server sends the XML packet of post messages to the URL filled in by the developer. · understanding: the wechat server returns the message sent by the user to req in the form of post stream. When we want to get the message sent by the user, we can use req Getinputstream(). Of course, we can perform necessary parsing according to the returned XML format of the message on the document. · achieve:
Send message to the document: this explains: when a user sends a message to the official account (or when some specific user action trigges an event push), a POST request is generated, and the developer can echo the message by returning a specific XML structure in the response packet (Get). (it now supports replying to text, pictures, graphics, voice, video and music). Strictly speaking, sending a passive response message is not an interface, but a reply to a message sent by the wechat server. · understanding: when a user sends a request, a post request will be generated, and we can reply to the message through response. However, the content of the reply is strictly qualified The wechat server will process and return to the user only if the format requirements are met. By viewing the document "message management" module, we can see that there are various messages in wechat. Each type of message has its own specific format requirements. We must return specific information to users normally according to the requirements. We try to reply users with text messages and graphic messages according to the required format of the document. Key: construct the required parameters according to the document requirements. Special note: parameters are case sensitive. · realize 1 - reply to ordinary text messages:
· realize 2-reply to graphic message:
All operation source codes in this part can be directly used • coreservlet Java (including server access, receiving messages sent by users, replying to ordinary text messages and replying to graphic messages. Third party jars: Dom4j and xStream are required)
•TestMsg. Java (plain text message bean)
•Article. Java (internal article bean of graphic message)
•PicAndTextMsg. Java (graphic message bean)
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.