http协议 get/post 请求 解析XML
http协议 get/post 请求 解析XML
当你拿到一个接口文档,要确定是协议接口。eg:HTTP协议 、WebService接口。
HTTP协议 URL为“http:”,这代表网页使用的是HTTP协议。
Get请求
// 根据地址获取请
HttpGet get = new HttpGet(getUrl);//这里发送get// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient(); String status = null; // 请求返回状态码 String reason = null; // 返回原因 String exchangeResult = null; try { // 通过请求对象获取响应对象 HttpResponse response = httpClient.execute(get); System.out.println("请求状态码:【" + response.getStatusLine().getStatusCode() + "】"); // 判断网络连接状态码是否正常(0--200都数正常) if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**
<result>
<Return Status="Success" Reason="" />
</result>
-- response.getEntity, 但当 exchangeResult 接收时为:<result><Return Status="Success" Reason="" /></result>
**/ exchangeResult = EntityUtils.toString(response.getEntity(),"utf-8"); exchangeResult = exchangeResult.replaceAll("<", "<").replaceAll(">", ">"); // xml数据返回含有特殊字符,这里只考虑了 < 和 > 号 Document doc = DocumentHelper.parseText(exchangeResult); Element root = doc.getRootElement(); // Element DOM Iterator<Element> iter = root.elementIterator(); while(iter.hasNext()){ Element tmpElement = (Element) iter.next(); // Return 节点 status = tmpElement.attributeValue("Status"); // Success reason = tmpElement.attributeValue("Reason"); } System.out.println("get获取结果返回--status:【" + status + "】reason:【" +reason + "】"); } } catch (Exception ex) { ex.printStackTrace(); }
Post请求
post请求一般用来推送数据,用多人会有疑问,要怎么样构造自己想要的xml eg: ![CDATA[{字段值}]]
Element root = DocumentHelper.createElement("Root"); Element entity = root.addElement("Entity").addAttribute("EntityName", "book").addAttribute("remark", "false"); entity.addElement("Property").addAttribute("Name", "num").addCDATA("123"); String xmlInfo = "<?xml version='1.0' encoding='utf-8'?>" + root.asXML();
结果:
<?xml version='1.0' encoding='utf-8'?>
<Root>
<Entity EntityName='book' remark='false' >
<Property Name='num'><![CDATA[123]]></Property>
</Entity>
</Root>
String data = postData.toString(); // xml 数据 HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(postUrl); String exchangeResult = null; try { RequestEntity entity = new StringRequestEntity(data, "text/plain","utf-8"); post.setRequestEntity(entity); httpclient.executeMethod(post); // post 请求 int code = post.getStatusCode(); if (code == HttpStatus.SC_OK){ }
// 解析上同 exchangeResult = new String(post.getResponseBodyAsString()); //接口返回的信息 exchangeResult = exchangeResult.replaceAll("<", "<").replaceAll(">", ">"); Document doc = DocumentHelper.parseText(exchangeResult); Element root = doc.getRootElement(); Iterator<Element> iter = root.elementIterator(); String status = null; String reason = null; while(iter.hasNext()){ Element tmpElement = (Element) iter.next(); status = tmpElement.attributeValue("Status"); reason = tmpElement.attributeValue("Reason"); } System.out.println("市接口推送过程数据结果:status:【" + status +"】,reason:【" + reason + "】"); } catch (Exception ex) { ex.printStackTrace(); } finally { post.releaseConnection(); }
有些人会有疑问为什么不转为json来解析数据,但看我们红色的xml数据,我们请求的时,返回的是String xml格式数据,但json强调的是 getString(“Status“) 返回一个text值,并不是参数属性值。eg:<property name=’status’>Success</property> ,
当这个情况时转为json会取得status。