Java的微信开发中使用XML格式和JSON格式数据的详解及实例

高洛峰
发布: 2017-03-28 14:17:11
原创
2608人浏览过

这篇文章主要介绍了Java微信开发中使用XML格式和JSON格式数据的示例,注意一下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 {
 
 /**
  * 根据注册信息,获得的参数,提交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;
 }
}
登录后复制

额 大体就是这样

以上就是Java的微信开发中使用XML格式和JSON格式数据的详解及实例的详细内容,更多请关注php中文网其它相关文章!

微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号