XML
WeChat XMLメッセージモデル定義:
パッケージ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 2014年5月21日2:13:27 pm */public class xmlmsg {//通常のメッセージ基本的な変数文字列tousname; String FromUsername;文字列createTime;文字列msgtype;文字列コンテンツ。文字列msgid; //イベントプッシュ変数文字列イベント; //カスタムメニュー項目文字列イベントキー。 public string getEventKey(){return eventKey; } public void seteventKey(string eventKey){eventKey = eventKey; } public xmlmsg(string str)throws documentexception {document doc = documenthelper.parsetext(str);要素root = doc.getRootelement(); this.tousername = root.ElementText( "tousername"); this.fromusername = root.ElementText( "fromUsername"); this.createtime = root.ElementText( "CreateTime"); 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 tousername; } public void setoudername(string tousername){tousername = 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
ここでは、JSON-LIBを使用します。次のJARパッケージが必要であることに注意してください。
以下は、文字列タイプのaccess_tokenを返すシンプルなAccessTokenクラスです
パッケージcn.wx.server; 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; net.sf.json.jsonobjectをインポートします。パブリッククラスAccessToken { / ** *登録情報に従って、取得したパラメーターは、accesstkoen * @author lpe234 * @time 2014-5-21 00:52:15 * / string appid = "xxxxxxxxxxxxxxxxxxxxxx"; String AppSecret = "xxxxxxxxxxxxxxxx 。 。 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();*}* // return string inger temp = getjson(); jsonobject j = jsonobject.fromobject(temp); temp = j.getString( "Access_token"); //system.out.println(temp); return temp; } // wxサーバーを取得しますjsonデータを返します。 try {url url = new url(tempurl); urlconnection conn = url.openconnection(); inputStreamReader ISR = new inputStreamReader(conn.getInputStream()); BufferedReader BR = new BufferedReader(ISR); temp = br.readline(); } catch(malformedurlexception e){// dodo auto-enerated catch block e.printstacktrace(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); } //system.out.println(temp); return temp; }}まあ、それは基本的にそれです