Java的微信开发中使用XML格式和JSON格式数据的示例
XML
微信XML消息model定义:
packagecn.wx.server; importorg.dom4j.Document; importorg.dom4j.DocumentException; importorg.dom4j.DocumentHelper; importorg.dom4j.Element; /** *@titlecn.wx.serverXMLMsg.java *@todoTODO *@authorlpe234 *@time2014年5月21日下午2:13:27 */ publicclassXMLMsg{ //普通消息基本变量 StringToUserName; StringFromUserName; StringCreateTime; StringMsgType; StringContent; StringMsgId; //事件推送变量 StringEvent; //自定义菜单项 StringEventKey; publicStringgetEventKey(){ returnEventKey; } publicvoidsetEventKey(StringeventKey){ EventKey=eventKey; } publicXMLMsg(Stringstr)throwsDocumentException{ Documentdoc=DocumentHelper.parseText(str); Elementroot=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"); } publicStringgetEvent(){ returnEvent; } publicvoidsetEvent(Stringevent){ Event=event; } publicStringgetToUserName(){ returnToUserName; } publicvoidsetToUserName(StringtoUserName){ ToUserName=toUserName; } publicStringgetFromUserName(){ returnFromUserName; } publicvoidsetFromUserName(StringfromUserName){ FromUserName=fromUserName; } publicStringgetCreateTime(){ returnCreateTime; } publicvoidsetCreateTime(StringcreateTime){ CreateTime=createTime; } publicStringgetMsgType(){ returnMsgType; } publicvoidsetMsgType(StringmsgType){ MsgType=msgType; } publicStringgetContent(){ returnContent; } publicvoidsetContent(Stringcontent){ Content=content; } publicStringgetMsgId(){ returnMsgId; } publicvoidsetMsgId(StringmsgId){ 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
packagecn.wx.server; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; importjava.net.MalformedURLException; importjava.net.URL; importjava.net.URLConnection; importnet.sf.json.JSONObject; publicclassAccessToken{ /** *根据注册信息,获得的参数,提交get请求,获得accessTkoen *@authorlpe234 *@time2014-5-2100:52:15 */ StringappID="XXXXXXXXXXXXXX"; Stringappsecret="XXXXXXXXXXXXXXXXX";//微信服务号或者申请测试账号的订阅号才有。。。 StringpreUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; StringtempUrl=String.format(preUrl,appID,appsecret); /**测试 *publicstaticvoidmain(String[]args){ *AccessTokenas=newAccessToken(); *System.out.println(as.get()); *} */ //返回String类型access_token publicStringget(){ Stringtemp=null; temp=getJSON(); JSONObjectj=JSONObject.fromObject(temp); temp=j.getString("access_token"); //System.out.println(temp); returntemp; } //获取wx服务器返回JSON数据,private内部调用 privateStringgetJSON(){ Stringtemp=null; try{ URLurl=newURL(tempUrl); URLConnectionconn=url.openConnection(); InputStreamReaderisr=newInputStreamReader(conn.getInputStream()); BufferedReaderbr=newBufferedReader(isr); temp=br.readLine(); }catch(MalformedURLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } //System.out.println(temp); returntemp; } }
额大体就是这样