JAVA调用 net webservice 接口 解析XML数据 实用小功能
本次调用使用XML格式传输
接口客户端调用接口 使用HTTP协议处理, 本文通过对整个流程的解释,希望对大家有帮助
1 获取.NET发布接口地址
FE:http://IP:port/WebService/getinfo.asmx/getUserInfo
2 HTTP协议功能函数并示范调用
通用HTTP协议请求.net webservice 接口函数:
//请求URI 参数列表(a=1&n=3... ...)
public String sendPostRequestTest(String requestUri, String params)
{
URL url = null;
HttpURLConnection con = null;
StringBuffer json = new StringBuffer();
DataOutputStream out = null;
BufferedReader reader = null;
InputStream is = null;
try{
url = new URL(requestUri);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
// 超时设置暂时定5s
// con.setReadTimeout(5000);
con.setConnectTimeout(10000);
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Accept-Charset", "utf-8");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
//设置参数
con.setRequestProperty("Content-Length",
String.valueOf(params.getBytes().length));
//发送参数 等待返回数据
con.setDoOutput(true);
con.setDoInput(true);
out = new DataOutputStream(con.getOutputStream());
out.write(params.getBytes("UTF-8"));
out.close();
if (con.getResponseCode() == 200) {
is = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
//处理接口返回数据
String param;
while ((param = reader.readLine()) != null) {
json.append(param);
}
reader.close();
is.close();
} else {
System.out.println("服务器返回状态值:" + con.getResponseCode());
}
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭链接 必须
con.disconnect();
}
return json.toString();
}
3 针对接口返还数据进行解析(可以使用反射处理或单独字段处理,此处提供字段针对的解析)
经过上步骤 已经获取了XML 数据:
public List<CancelContract> processXMLstr(String xmlStr) {
List<CancelContract> list = null;
try {
Document doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
Iterator body = root.elementIterator("diffgram");
if (body.hasNext()) {
list = new ArrayList<CancelContract>();
// 获取数据层
Element recordEless = (Element) body.next();
Element e = recordEless.element("NewDataSet");
// table 层
List es = e.elements("Table");
// 如此获取到了 数据表层
Element tempElement = null;
Node att = null;
CancelContract cc = null;
Iterator entityAtts = null;
// 遍历 每个TABLE 对象列表
for (int i = 0; i < es.size(); i++) {
cc = new CancelContract();
tempElement = (Element) es.get(i);
entityAtts = tempElement.nodeIterator();
while (entityAtts.hasNext()) {
att = (Node) entityAtts.next();
if (att.getName() == null)
continue;
if ("cancel_contract_code".equalsIgnoreCase(att
.getName())) {
cc.setCancel_contract_code(att.getStringValue());
} else if ("cancel_dev_code".equalsIgnoreCase(att
.getName())) {
cc.setCancel_dev_code(att.getStringValue());
}
}
list.add(cc);
}
es = null;
tempElement = null;
att = null;
cc = null;
entityAtts = null;
recordEless = null;
e = null;
doc = null;
root = null;
body = null;
}
return list;
} catch (Exception e) {
System.out.println("解析数据出现错误");
//e.printStackTrace();
}
return list;
}
4 返回处理的数据到浏览器前台
到这里数据已经变为了JAVA 对象数据,我们就可以通过 HttpServletRequest 包装一下 带回到动态界面里。
5 页面CSS样式优化
6 调用流程完成。
如有疑问 请留言 欢迎提供建议
评论已有 0 条