Current location - Quotes Website - Signature design - How to develop WeChat with java
How to develop WeChat with java

Note:

This tutorial is mainly an explanation of the WeChat public platform developer model. There are many similar articles on the Internet, but many of them are confusing to those who are new to WeChat development. , so I summarized my WeChat development experience, systematically listed the entire process of WeChat development, and explained and analyzed the main codes so that beginners can get started as soon as possible.

Before reading this article, you should have some understanding of the official development documents of the WeChat public platform, and know that all data received and sent is in XML format. In addition, the Turing Robot's API interface is used when replying to content. This is an open platform for natural language parsing, which can help us solve the most difficult problems in the entire WeChat development process. I won't go into details here, but there are others below. Detailed calling method.

1.1 After logging into the official WeChat platform, turn on the developer mode. At this time, we need to fill in the url and token. The so-called url is the interface of our own server, which is implemented with WechatServlet.java. The relevant explanation is already in the comments. Description, the code is as follows:

[java]?view plain?copy

package?demo.servlet;?

import?java.io.BufferedReader; ?

import?java.io.IOException;?

import?java.io.InputStream;?

import?java.io.InputStreamReader;?< /p>

import?java.io.OutputStream;?

import?javax.servlet.ServletException;?

import?javax.servlet./xml/ns/javaee "

xmlns:xsi="/xml/ns/javaee

/xml/ns/javaee/web-app_2_5.xsd">?

?

This?is?the?description?of?my?J2EE?component?

This?is?the? display?name?of?my?J2EE?component?

WechatServlet?

demo.servlet.WechatServlet?

?

?

WechatServlet ?

/wechat.do?

?

?

index.jsp?

?

?

1.3 Through the above code, we have implemented the framework of WeChat public platform development, that is, opening the developer mode and successfully accessing, receiving messages and sending messages. Three steps.

The following will explain its core part - parsing the received xml data, taking text messages as an example, and implementing intelligent replies through the Turing robot api interface.

2.1 First, let’s take a look at the overall process processing code, including: xml data processing, calling Turing API, and encapsulating the returned xml data.

[java]?view plain?copy

package?demo.process;?

import?java.util.Date;?

< p>import?demo.entity.ReceiveXmlEntity;?

/**?

*?WeChat xml message processing process logic class?

*?@author ?pamchen-1?

*?

*/?

public?class?WechatProcess?{?

/**?

*?Parse and process xml and obtain intelligent reply results (through Turing robot api interface)?

*?@param?xml?Received WeChat data?

*?@return?Final parsing result (xml format data)?

*/?

public?String?processWechatMag(String?xml){?

/**?Parse xml data?*/?

ReceiveXmlEntity?xmlEntity?=?new?ReceiveXmlProcess().getMsgEntity(xml);?

/** ?Take text messages as an example, call the Turing robot api interface to obtain the reply content?*/?

String?result?=?"";?

if("text" .endsWith(xmlEntity.getMsgType())){?

result?=?new?TulingApiProcess().getTulingResult(xmlEntity.getContent());?

}?

/**?At this time, if the user inputs "Hello", after going through the above process, the result will be something similar to "Hello"?

*?Because The final reply to WeChat is also in XML format, so it needs to be encapsulated as a text type to return the message?

*?*/?

result?=?new?FormatXmlProcess() .formatXmlAnswer(xmlEntity.getFromUserName(),?xmlEntity.getToUserName(),?result);?

return?result;?

}?

} ?

2.2 Parse the received xml data. There are two classes here, ReceiveXmlEntity.java and ReceiveXmlProcess.java. The set method in the entity class is dynamically called through the reflection mechanism, which can avoid many repeated judgments. , to improve code efficiency, the code is as follows:

[java]?view plain?copy

package?demo.entity;?

/**?

*?The received WeChat xml entity class?

*?@author?pamchen-1?

*?

*/?

public?class?ReceiveXmlEntity?{?

private?String?ToUserName="";?

private?String?FromUserName="";?< /p>

private?String?CreateTime="";?

private?String?MsgType="";?

private?String?MsgId="";?

private?String?Event="";?

private?String?EventKey="";?

private?String?Ticket=""; ?

private?String?Latitude="";?

private?String?Longitude="";?

private?String?Precision="" ;?

private?String?PicUrl="";?

private?String?MediaId="";?

private?String?Title=" ";?

private?String?Description="";?

private?String?Url="";?

private?String?Location_X= "";?

private?String?Location_Y="";?

private?String?Scale="";?

private?String?Label ="";?

private?String?Content="";?

private?String?Format="";?

private?String? Recognition="";?

public?String?getRecognition()?{?

return?Recognition;?

}?

public?void?setRecognition(String?recognition)?{?

Recognition?=?recognition;?

}?

public?String?getFormat( )?{?

return?Format;?

}?

public?void?setFormat(String?format)?{?

< p>Format?=?format;?

}?

public?String?getContent()?{?

return?Content;?

}?

public?void?setContent(String?content)?{?

Content?=?content;?

}?< /p>

public?String?getLocation_X()?{?

return?Location_X;?

}?

public?void?setLocation_X( String?locationX)?{?

Location_X?=?locationX;?

}?

public?String?getLocation_Y()?{?

return?Location_Y;?

}?

public?void?setLocation_Y(String?locationY)?{?

Location_Y?=?locationY ;?

}?

public?String?getScale()?{?

return?Scale;?

}?< /p>

public?void?setScale(String?scale)?{?

Scale?=?scale;?

}?

public ?String?getLabel()?{?

return?Label;?

}?

public?void?setLabel(String?label)?{?

Label?=?label;?

}?

public?String?getTitle()?{?

return?Title ;?

}?

public?void?setTitle(String?title)?{?

Title?=?title;?

< p>}?

public?String?getDescription()?{?

return?Description;?

}?

public ?void?setDescription(String?description)?{?

Description?=?description;?

}?

public?String?getUrl()? {?

return?Url;?

}?

public?void?setUrl(String?url)?{?

Url?=?url;?

}?

public?String?getPicUrl()?{?

return?PicUrl;?

< p>}?

public?void?setPicUrl(String?picUrl)?{?

PicUrl?=?picUrl;?

}?

public?String?getMediaId()?{?

return?MediaId;?

}?

public?void?setMediaId(String? mediaId)?{?

MediaId?=?mediaId;?

}?

public?String?getEventKey()?{?

< p>return?EventKey;?

}?

public?void?setEventKey(String?eventKey)?{?

EventKey?=?eventKey;?

}?

public?String?getTicket()?{?

return?Ticket;?

}?

public?void?setTicket(String?ticket)?{?

Ticket?=?ticket;?

}?

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;?

}?

public?String?getEvent()?{?

return?Event;?

}?

public?void?setEvent(String?event)?{?

Event? =?event;?

}?

public?String?getMsgId()?{?

return?MsgId;?

}?

public?void?setMsgId(String?msgId)?{?

MsgId?=?msgId;?

}?

< p>public?String?getToUserName()?{?

return?ToUserName;?

}?

public?void?setToUserName(String?toUserName) ?{?