Xml
Définition du modèle de message XML WeChat:
package cn.wx.server; import org.dom4j.document; import org.dom4j.documentException; import org.dom4j.documenthelper; import org.dom4j.element; / ** * @title cn.wx.serverxmlmsg.java * @todo todo * @author lpe234 * @time 21 mai 2014 à 2:13:27 pm * / public class xmlmsg {// message normal Message de base variable String Tousername; String FromUserName; String CreateTime; String msgType; String Content; String msgid; // Event push push variable string event; // Menu élément de menu personnalisé String EventKey; public String getEventKey () {return EventKey; } public void SetEventKey (String EventKey) {EventKey = EventKey; } public xmlmsg (String Str) lève DocumentException {document doc = documentHelper.parseText (str); Élément root = doc.getrootelement (); this.tousername = root.elementText ("Tausername"); this.fromUsername = root.elementText ("FromUserName"); this.CreateTime = root.ElementText ("CreateEtime"); this.msgType = root.ElementText ("msgType"); this.Content = root.ElementText ("Content"); this.msgid = root.elementText ("msgid"); this.event = root.elementText ("Event"); this.eventKey = root.elementText ("EventKey"); } public String getEvent () {return event; } public void setEvent (string event) {event = event; } public String getTousername () {return Tausername; } public void Settousename (String Tousername) {Tausername = Tousername; } public String getFromUsername () {return fromUserName; } public void setFromUsername (String fromUserName) {FromUserName = FromUserName; } public String getCreateTime () {return CreateTime; } public void SetCreateTime (String CreateTime) {CreateTime = CreateTime; } public String getmsgType () {return msgType; } public void SetMsGType (String msgType) {msgType = msgType; } public String getContent () {return Content; } public void setContent (String Content) {Content = Content; } public String getmsgid () {return msgid; } public void setmsgid (string msgid) {msgid = msgid; }}
Json
Ici, nous utilisons JSON-lib, notez que les packages de pot suivants sont nécessaires:
Ce qui suit est une classe AccessToken simple qui renvoie l'accès_token du type de chaîne
package cn.wx.server; Importer java.io.buffereDReader; import java.io.ioException; import java.io.inputStreamReader; import java.net.malformedUrlexception; import java.net.url; import java.net.url; import java.net.urlconnection; importer net.sf.json.jsonObject; classe publique AccessToken {/ ** * Selon les informations d'enregistrement, les paramètres obtenus, soumettent une demande GET pour obtenir AccessTkoen * @Author LPE234 * @time 2014-5-21 00:52:15 * / String appid = "xxxxxxxxxxxxxxx"; String AppSecret = "XXXXXXXXXXXXXXX"; // Vous seul avez un compte de service WeChat ou un compte d'abonnement qui s'applique à un compte de test. . . String preurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%S&secret=%S"; String tempurl = string.format (preurl, appid, appSecret); / ** test * public static void main (string [] args) {* AccessToken as = new AccessToken (); * System.out.println (as.get ()); *} * / // return String type access_token public String get () {String temp = null; temp = getjson (); JSONObject j = jsonObject.FromObject (temp); temp = j.getString ("Access_token"); //System.out.println(Temp); Tempère de retour; } // Obtenez le serveur WX renvoie les données JSON, appelez privé String privé getjson () {String temp = null; essayez {url url = new url (tempurl); UrlConnection Conn = url.OpenConnection (); InputStreamReader ISR = new inputStreamReader (Conn.getInputStream ()); BufferedReader br = new BufferedReader (ISR); temp = br.readline (); } catch (MalformEdUrException e) {// TODO Générations de catch Généra E.PrintStackTrace (); } catch (ioException e) {// TODO Bloc de capture généré automatiquement e.printStackTrace (); } //System.out.println(Temp); Tempère de retour; }}Eh bien, c'est essentiellement ça