其实就是一个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;
}
}
}
下面是代码 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;
}
}
}
解决方案 »
- android开发不会自动选择图片分辨率啊
- 基站定位问题
- 有没有办法知道自己的程序是在模拟器上运行的还是在真实机器上运行的?
- 如何对短信的threads表进行操作?
- 100分 Android SeekBar问题
- 求助!关于Android Bluetooth
- ContentProvider中怎样实例化dbHelper?求解。。快哭了T_T...
- 看黑马程序员,写天气解析的时候程序调不出来。帮我看看吧
- android 程序按下home键后 到后台直接退出了。再次点击桌面图标打开是新开的,不会回到上次浏览的页面。
- Android中handler如何传list
- popupWindow对话框实现了自定义menu的效果
- 屏幕最大分辨率1024*768,android打印出来是800*600
List<Building> list = envelope. //这里不知道怎么写了
我的感觉 是通过envelope对象可以得到,但是不知道怎么写
转不了实体类
Bodyin的数据结构是这样的
getBuildingsByAccountNameResponse{return=anyType{pid=1; pid=1; name=building1; }; return=anyType{pid=2; pid=2; name=building2; }; }
我要的是anyType大括号抱住的两段,实体类中三个属性,也就是两条数据
1.String result=object.toString()
2.result作为一个String类型的xml文件流 ,然后再使用SAX读取这个xml文件 ,你主要要做的是如何用SAX去解析、操作。
那个东西,貌似没有包可以得到List<T>
后来我们用HTTP协议了
Server: Vector<T> vec = new Vector();
vec.addAll( List<T> );
Android: Vector<SoapObject> vec = (Vector<SoapObject>) envelope..getResponse(); 这句容易出类转换异常,我也不知道哪的问题,别人运行的没问题--!
for (SoapObject list : vec) {
list.getProperty( );
}