Android通过post请求发送一个xml,解析返回xml数据
2015-08-12 15:50
531 查看
工作的时候需要往后台发送一个post数据请求
其中发送的xml数据为:
<?xml version = “1.0” ?>
<SSOMessage version=”1.0”>
<SSOParas>
<SeqID>SeqID</SeqID>
<CommandID>CommandID</CommandID>
<MSISDN>ABSCDSDF</MSISDN>
<ChargeMSISDN>ChargeMSISDN</ChargeMSISDN>
<SPID>SPID</SPID>
<Code> Code </ Code >
< IDtype > IDtype 0</ IDtype >
<ID> ID 0</ID>
</SSOParas>
</SSOMessage>
返回的xml数据为:
<?xml version = “1.0” ?>
<SSOMessage version=”1.0”>
<SSOParas>
<SeqID>SeqID</SeqID>
<ResultCode>ResultCode0</ResultCode>
</SSOParas>
</SSOMessage>
然后进行解析,代码如下,参考一下,对于以后再做post请求的时候,做参考
class
httpThread implements Runnable {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
//组建xml数据
StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<SSOMessage version=\"1.0\">");
xml.append("<SSOParas>");
xml.append("<SeqID>13333333333</SeqID>");
xml.append("<CommandID>1</CommandID>");
xml.append("<MSISDN>1333333333</MSISDN>");
xml.append("<ChargeMSISDN>1333333333</ChargeMSISDN>");
xml.append("<SPID>3510127</SPID>");
xml.append("<Code></Code>");
xml.append("<IDtype>0</IDtype>");
xml.append("<ID>135000000000000216559</ID>");
xml.append("</SSOParas>");
xml.append("</SSOMessage>");
try {
byte[] xmlbyte = xml.toString().getBytes("UTF-8");
System.out.println(xml);
URL url = new URL("http://118.85.194.28:8080/sotpms_server/GetSSOMessage");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 允许输出
conn.setDoInput(true);
conn.setUseCaches(false);// 不使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(xmlbyte.length));
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("X-ClientType", "2");//发送自定义的头信息
conn.getOutputStream().write(xmlbyte);
conn.getOutputStream().flush();
conn.getOutputStream().close();
if (conn.getResponseCode() != 200)
throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();// 获取返回数据
// 使用输出流来输出字符(可选)
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
String string = out.toString("UTF-8");
System.out.println(string);
out.close();
// xml解析
String version = null;
String seqID = null;
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new ByteArrayInputStream(string.substring(1)
.getBytes("UTF-8")), "UTF-8");
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("SSOMessage".equals(parser.getName())) {
version = parser.getAttributeValue(0);
} else if ("SeqID".equals(parser.getName())) {
seqID = parser.nextText();
} else if ("ResultCode".equals(parser.getName())) {
resultCode = parser.nextText();
}
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
System.out.println("version = " + version);
System.out.println("seqID = " + seqID);
System.out.println("resultCode = " + resultCode);*/
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
其中发送的xml数据为:
<?xml version = “1.0” ?>
<SSOMessage version=”1.0”>
<SSOParas>
<SeqID>SeqID</SeqID>
<CommandID>CommandID</CommandID>
<MSISDN>ABSCDSDF</MSISDN>
<ChargeMSISDN>ChargeMSISDN</ChargeMSISDN>
<SPID>SPID</SPID>
<Code> Code </ Code >
< IDtype > IDtype 0</ IDtype >
<ID> ID 0</ID>
</SSOParas>
</SSOMessage>
返回的xml数据为:
<?xml version = “1.0” ?>
<SSOMessage version=”1.0”>
<SSOParas>
<SeqID>SeqID</SeqID>
<ResultCode>ResultCode0</ResultCode>
</SSOParas>
</SSOMessage>
然后进行解析,代码如下,参考一下,对于以后再做post请求的时候,做参考
class
httpThread implements Runnable {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
//组建xml数据
StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<SSOMessage version=\"1.0\">");
xml.append("<SSOParas>");
xml.append("<SeqID>13333333333</SeqID>");
xml.append("<CommandID>1</CommandID>");
xml.append("<MSISDN>1333333333</MSISDN>");
xml.append("<ChargeMSISDN>1333333333</ChargeMSISDN>");
xml.append("<SPID>3510127</SPID>");
xml.append("<Code></Code>");
xml.append("<IDtype>0</IDtype>");
xml.append("<ID>135000000000000216559</ID>");
xml.append("</SSOParas>");
xml.append("</SSOMessage>");
try {
byte[] xmlbyte = xml.toString().getBytes("UTF-8");
System.out.println(xml);
URL url = new URL("http://118.85.194.28:8080/sotpms_server/GetSSOMessage");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 允许输出
conn.setDoInput(true);
conn.setUseCaches(false);// 不使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(xmlbyte.length));
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("X-ClientType", "2");//发送自定义的头信息
conn.getOutputStream().write(xmlbyte);
conn.getOutputStream().flush();
conn.getOutputStream().close();
if (conn.getResponseCode() != 200)
throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();// 获取返回数据
// 使用输出流来输出字符(可选)
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
String string = out.toString("UTF-8");
System.out.println(string);
out.close();
// xml解析
String version = null;
String seqID = null;
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new ByteArrayInputStream(string.substring(1)
.getBytes("UTF-8")), "UTF-8");
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("SSOMessage".equals(parser.getName())) {
version = parser.getAttributeValue(0);
} else if ("SeqID".equals(parser.getName())) {
seqID = parser.nextText();
} else if ("ResultCode".equals(parser.getName())) {
resultCode = parser.nextText();
}
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
System.out.println("version = " + version);
System.out.println("seqID = " + seqID);
System.out.println("resultCode = " + resultCode);*/
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
相关文章推荐
- 关于android中broadcast和BroadcastReceiver浅谈
- Android应用程序资源的编译和打包过程分析 (转自老罗的博客)
- Android View事件分发和处理
- android 关于蓝牙间通信,及RFID设备读写芯片问题的处理方案
- Android生命周期
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控缩放
- Android ColorFilter:PorterDuffColorFilter、LightingColorFilter、ColorMatrixColorFilter
- Android中Calendar与Date的区别以及消除时区对日期操作影响的方法
- 【自定义控件】android事件分发机制
- android之view测量总结
- 如何学习android高级编程
- 关于Android数据存储方式(二)——SQLite
- 每个Android开发者都应该了解的资源列表
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
- Android WebView与JavaScript交互实现Web App
- 如何快速学习android开发
- android 拍卖项目
- Android Studio使用遇到的问题
- android studio中ddms显示不出来,报错
- android 之sqlite 使用及实现增、删、改、查 并使用AndroidTestCase 进行junit 测试