Java의 위챗 개발에서 XML 형식과 JSON 형식의 데이터를 사용하는 예

4827 단어 Java위챗
XML
위챗 XML 메시지 모델 정의:

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 2014 5 21 2:13:27
 */
public class XMLMsg {
// 
 String ToUserName;
 String FromUserName;
 String CreateTime;
 String MsgType;
 String Content;
 String MsgId;
// 
 String Event;
// 
 String EventKey;
  
 
 public String getEventKey() {
  return EventKey;
 }
 
 public void setEventKey(String eventKey) {
  EventKey = eventKey;
 }
 
 public XMLMsg(String str) throws DocumentException {
  Document doc = DocumentHelper.parseText(str);
  Element 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 setToUserName(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 패키지의 지원이 필요합니다.
  • json-lib-2.4-jdk15.jar
  • commons-logging-1.1.3.jar
  • ezmorph-1.0.6.jar
  • commons-lang-2.4.jar
  • commons-collections.jar
  • commons-beanutils-1.8.0.jar
  • 다음은 간단한 AccessToken 클래스입니다. String 형식의 access_를 되돌려줍니다.token
    
    package cn.wx.server;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
     
    import net.sf.json.JSONObject;
     
    public class AccessToken {
     
     /**
      *  , , get , accessTkoen
      * @author lpe234
      * @time 2014-5-21 00:52:15
      */
     String appID = "XXXXXXXXXXXXXX";
     String appsecret = "XXXXXXXXXXXXXXXXX";// 。。。
     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);
    /**  
    * public static void main(String[] args) {
    *  AccessToken as = new AccessToken();
    *  System.out.println(as.get());
    * }
    */
     // String access_token
     public String get() {
      String temp = null;
      temp = getJSON();
      JSONObject j = JSONObject.fromObject(temp);
      temp = j.getString("access_token");
      //System.out.println(temp);
      return temp;
     }
     
     //  wx JSON ,private 
     private String getJSON() {
      String temp = null;
      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) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      //System.out.println(temp);
      return temp;
     }
    }
    
    어, 대체로 이렇다.

    좋은 웹페이지 즐겨찾기