欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java的微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例

 更新時間:2016年02月28日 16:00:45   作者:明天以后  
這篇文章主要介紹了Java微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例,注意一下json-lib所需要的jar包,需要的朋友可以參考下

XML
微信XML消息model定義:

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 {
 
 /**
  * 根據(jù)注冊信息,獲得的參數(shù),提交get請求,獲得accessTkoen
  * @author lpe234
  * @time 2014-5-21 00:52:15
  */
 String appID = "XXXXXXXXXXXXXX";
 String appsecret = "XXXXXXXXXXXXXXXXX";//微信服務(wù)號或者申請測試賬號的訂閱號才有。。。
 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服務(wù)器返回JSON數(shù)據(jù),private內(nèi)部調(diào)用
 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;
 }
}

額 大體就是這樣

相關(guān)文章

  • 詳解前后端分離之Java后端

    詳解前后端分離之Java后端

    這篇文章主要介紹了詳解前后端分離之Java后端,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java日常練習(xí)題,每天進步一點點(30)

    Java日常練習(xí)題,每天進步一點點(30)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • java連接池Druid連接回收DestroyConnectionThread&DestroyTask

    java連接池Druid連接回收DestroyConnectionThread&DestroyTask

    這篇文章主要為大家介紹了java連接池Druid連接回收DestroyConnectionThread&DestroyTask示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • java多線程消息隊列的實現(xiàn)代碼

    java多線程消息隊列的實現(xiàn)代碼

    本篇文章主要介紹了java多線程消息隊列的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 使用Spring事件監(jiān)聽機制實現(xiàn)跨模塊調(diào)用的步驟詳解

    使用Spring事件監(jiān)聽機制實現(xiàn)跨模塊調(diào)用的步驟詳解

    Spring 事件監(jiān)聽機制是 Spring 框架中用于在應(yīng)用程序的不同組件之間進行通信的一種機制,Spring 事件監(jiān)聽機制基于觀察者設(shè)計模式,使得應(yīng)用程序的各個部分可以解耦,提高模塊化和可維護性,本文給大家介紹了使用Spring事件監(jiān)聽機制實現(xiàn)跨模塊調(diào)用,需要的朋友可以參考下
    2024-06-06
  • Java中的阻塞隊列BlockingQueue使用詳解

    Java中的阻塞隊列BlockingQueue使用詳解

    這篇文章主要介紹了Java中的阻塞隊列BlockingQueue使用詳解,阻塞隊列是一種線程安全的數(shù)據(jù)結(jié)構(gòu),用于在多線程環(huán)境下進行數(shù)據(jù)交換,它提供了一種阻塞的機制,當隊列為空時,消費者線程將被阻塞,直到隊列中有數(shù)據(jù)可供消費,需要的朋友可以參考下
    2023-10-10
  • Spring Cloud引入Eureka組件,完善服務(wù)治理

    Spring Cloud引入Eureka組件,完善服務(wù)治理

    這篇文章主要介紹了Spring Cloud引入Eureka組件,完善服務(wù)治理的過程詳解,幫助大家更好的理解和使用spring cloud,感興趣的朋友可以了解下
    2021-02-02
  • Java 加密解密基礎(chǔ)分類及模式歸納整理

    Java 加密解密基礎(chǔ)分類及模式歸納整理

    這篇文章主要介紹了Java加密解密基礎(chǔ)分類方法匯總的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • Maven之pom.xml文件中的Build配置解析

    Maven之pom.xml文件中的Build配置解析

    這篇文章主要介紹了Maven之pom.xml文件中的Build配置解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論