Examples of Java micro-credit development using XML format and JSON format data _java

Source: Internet
Author: User

XML
micro-letter XML message Model definition:

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 May 21, 2014 afternoon 2:13:27 * * public class Xmlmsg
 {//Normal message basic variable String tousername;
 String Fromusername;
 String Createtime;
 String Msgtype;
 String Content;
String MsgId;
Event push variable String event;
  
 
 Custom menu item 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
Here we use Json-lib, and note that we need support for the following jar packages:

    • 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

The following is a simple Accesstoken class that returns the string type of 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 {/** * According to the registration information, obtain the parameters, submit GET request, obtain Accesstkoen * @author lpe234 * @time 2014-5-21 00:52:15
 */String AppID = "xxxxxxxxxxxxxx";
 String Appsecret = "xxxxxxxxxxxxxxxxx";//micro-letter service number or the subscription number of the application test account only ... 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);
 return temp; }
 
 //Get WX server to return JSON data, private internal call 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;
 }
}

The amount is roughly the same

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.