欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java的微信开发中使用XML格式和JSON格式数据的示例

程序员文章站 2024-03-09 13:44:11
xml 微信xml消息model定义: package cn.wx.server; import org.dom4j.document; impo...

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 {
 
 /**
  * 根据注册信息,获得的参数,提交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;
 }
}

额 大体就是这样