Android Ksoap2包调用WebService怎么得到List对象

寒江易雪_王孙 寒江易雪_王孙 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
其实就是一个WebService返回结果怎么转换成List<T>的问题,我是一直搞C#的,Java不熟
下面是代码

WebService方法原型
 List<Building>getBuildingsByAccountName(String accountName);

Java代码调用如下:
 String method = "getBuildingsByAccountName";//方法名称
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("accountName", String.valueOf("max")));

retrurn WSHelper.GetResponse(method,params);

下面是WSHelper类
public class WSHelper {
final static String WSUrl="http://192.168.5.25:8080/automation/services/equipService?wsdl";

private static String namespace = "http://webservice.device.automation.zhaohai.com/";
/*************************************
* 获取web services内容
* @param url
* @param params
* @return
*************************************/
@SuppressWarnings("deprecation")
public static List<Building> GetResponse(String method,List<BasicNameValuePair> params)
{
try
{
String url = WSUrl;
SoapObject request = new SoapObject(namespace, method);
for(int i=0,len=params.size();i<len;i++){
request.addProperty(params.get(i).getName(), params.get(i).getValue());
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
HttpTransportSE ht = new HttpTransportSE(url);
ht.call(null, envelope);
String result="";
List<Building> list;
if(envelope.getResponse() != null)
{
SoapObject soapObject = (SoapObject) envelope.getResponse();
//List<Building> list = envelope. //这里不知道怎么写了
//List<Building> list=soapObject.getClass();//这样写是错的
// 通过getProperty下面这种方法可以得到类中属性的值 ,但要重新实例化一个Building,就太耗资源了,
//所有肯定有直接获得List的方法
result += "building名称:" + soapObject.getProperty("name") + "\n";
result += "ID:" + soapObject.getProperty("pid") + "\n";
}
else result="no value";
return list;
} catch (Exception e) {
return null;
}
}
}

...全文
评论 11
收藏 已收藏
分享
返回顶部
寒江易雪_王孙
Android 80,490 成员 91,371 内容