个人笔记,大神勿喷
上代码:
public class WebService extends AsyncTask<Void, Void, SoapObject> {
private static String mMethodName; // 方法名称
private static List<BasicNameValuePair> mParams;
private static IWebServiceCallback mCallback;
public static void request(webMethod methodName,List<BasicNameValuePair> params, IWebServiceCallback callback) {
mMethodName = methodName.toString();
mParams = params;
mCallback = callback;
new WebService().execute();
}
@Override
protected SoapObject doInBackground(Void... params) {
try { <strong>//核心代码</strong>
// 创建参数对象
SoapObject request = new SoapObject(BeefConfig.NAMESPASE,mMethodName);
for (BasicNameValuePair valuePair : mParams) {
// 添加参数
request.addProperty(valuePair.getName(), valuePair.getValue());
}
// 创建返回对象
SoapSerializationEnvelope response = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// .net 访问
response.dotNet = true;
//设置链接参数对象
response.setOutputSoapObject(request);
//创建链接对象,设置链接超时
HttpTransportSE connection = new HttpTransportSE(BeefConfig.URL,BeefConfig.TIMEOUT);
//链接请求数据
connection.call(BeefConfig.NAMESPASE + mMethodName, response);
//获取数据
SoapObject soapObject = (SoapObject) response.getResponse();
//return (SoapObject) soapObject.getProperty(BeefConfig.XMLBODY);// 返回XML body节点数据
<span style="white-space:pre"> </span>return soapObject;
}catch (Exception e) {
Log.e(BeefConfig.TAG, "WebService.doInBackground’ "+e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(SoapObject result) {
super.onPostExecute(result);
if(result != null && result.getPropertyCount() >0){
String status = result.getProperty(BeefConfig.DATASTATUS).toString();
// 数据获取成功,返回数据
if(status.equals(BeefConfig.SUCCESS)){
mCallback.result(result);
}else{
mCallback.error(BeefConfig.FAIL);
}
}else {
mCallback.error("服务器链接超时");
}
}
}
/**
* 枚举 webservice 方法
*/
public static enum webMethod{
GetUserInfo, //方法(接口)名
GetByTZJH , //
}
以上是用 webservice 获取服务的代码
下面来解析数据,这里服务端返回的是 XML
原始XML:
<body>
<dataStatus> 1</dataStaus>
<data>
<username>zhangsan</username>
<password>1234<password/>
<name>三生石等一人</name>
</data>
</body>
XML 自己纯手工敲上去的,这是原始的XMl文件,WebService 会把XML封装成SoapObject 对象数据
下面就来解析 SoapObject 对象
单条数据解析
/**
* 解析用户数据
* @param soapObject
* @return
*/
public static UserBean analysisUserData(SoapObject soapObject){
if(soapObject == null){
return null;
}
SoapObject soap = (SoapObject)soapObject.getProperty(BeefConfig.DATA); //获取 data 节点的数据
UserBean user = new UserBean();
try {
user.setName(soap.getPropertyAsString("name"));
user.setPassword(soap.getPropertyAsString("password"));
user.setUsername(soap.getPropertyAsString("username"));
return user;
} catch (Exception e) {
Log.e(BeefConfig.TAG," XMLUtils.analysisUserData':"+e.getMessage());
}
return null;
}
/**
* 解析待审批计划
* @param soapObject
* @return
*/
public static List<PlanBean> analysisPlanData(SoapObject soapObject){
if(soapObject == null){
return null;
}
List<PlanBean> planList = new ArrayList<PlanBean>();
try {
// i=1 ,从data节点开始取数据,这样就不用解析上面的 <dataStatus> ,下面有具体解释
for (int i = 1; i < soapObject.getPropertyCount(); i++) {
SoapObject soap = (SoapObject) soapObject.getProperty(i);
PlanBean plan = new PlanBean();
plan.setSpdw(soap.getPropertyAsString("spdw"));
plan.setSpr(soap.getPropertyAsString("spr"));
plan.setSpsj(soap.getPropertyAsString("spsj"));
plan.setSpyj(soap.getPropertyAsString("spyj"));
planList.add(plan);
}
return planList;
} catch (Exception e) {
Log.e(BeefConfig.TAG, ".XMLUtils.analysisPlanData':"+e.getMessage());
}
return null;
}
以上就是 单条和多条数据的解析
注意以下这两个方法的区别:
SoapObject soap = (SoapObject) soapObject.getProperty(i); //多条数据的时候用
<strong></strong><pre name="code" class="java">SoapObject soap = (SoapObject)soapObject.getProperty(BeefConfig.DATA); //单条数据的时候用
单条SoapObject 数据格式
String soap = soapObject.getProperty(BeefConfig.DATA).toString();
body=anyType {
dataStatus=1,
data=anyType{username=zhangsan,password=1234,name=三生石等一人},
},
这就是SoapObject 数据格式了, body=anyType 表示这是一个SoapObject 对象并且,如果没有anyType标识, 在这么写SoapObject soap = (SoapObject) soapObject.getProperty(i); 就会抛异常,数据类型不同。
也就是说只有 XML 一个节点下面有子节点 ,这样的节点才是SoapObject 对象,否则就是属性。
一句话就是 有 anyType标识的节点,才能转换成SoapObject。
列表SoapObject数据格式
SoapObject soap = (SoapObject) soapObject.getProperty(i);
body=anyType {
dataStatus=1,
data=anyType{username=zhangsan,password=0004,name=三生石等一人1},
data=anyType{username=zhangsan,password=1111,name=三生石等一人2},
data=anyType{username=zhangsan,password=2222,name=三生石等一人3},
data=anyType{username=zhangsan,password=3333,name=三生石等一人4},
},
上面的数据格式看出来,有多个data 节点(多个用户),这里就不用 SoapObject soap = (SoapObject)soapObject.getProperty(BeefConfig.DATA); 这种方式去获取数据了 ,
如果有多个节点名相同,始终只能获取到最后一条数据, 这里就用 SoapObject soap = (SoapObject) soapObject.getProperty(i); 用下标循环获取下面的子节点。
总结: WebService获取数据就比较简单了,我就是在解析数据这里折腾了半天, WebService 就是将数据封装成 SoapObject 格式,这种格式很像 JSON 格式,只是不像JSON 有 JSONArray 。
1. soapObject.getProperty(BeefConfig.DATA); 获取单个节点,获取属性数据,要注意如果要转换成 SoapObject 对象,必须知道该节点有 anyType 标识,否则报错。
2. soapObject.getProperty(i); 根据下标获取数据,一般用于循环遍历一个节点下面的属性或子节点