具体问题是,我的Webservice的传入参数与返回值都是复杂的对象或对象数组,然而我用网上的代码做了以后,可以实现发送对象及对象数组,但是却无法用比较简单的办法解析返回对象,我要解析的对象已经实现了KvmSerializable接口了,可是还是无法通过强制转换的方式转成我自定义的对象,我实在不愿意用遍历SoapObject的方式生成对象啊,请各位高手不吝赐教。
自定义的对象testObject1
public class testObject1 implements KvmSerializable {
public String testName1;
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
return testName1;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
// TODO Auto-generated method stub
arg2.name = "testName1";
arg2.type = PropertyInfo.STRING_CLASS;
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
testName1 = arg1.toString();
}
}
调用代码,毕业论文 调用的Webservice没有入参,直接返回一个testObject1对象
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.bodyOut = soapObject;
soapEnvelope.setOutputSoapObject(soapObject);
soapEnvelope.addMapping(NAMESPACE, "testObject1", testObject1.class);
HttpTransportSE httpTransportSE = new HttpTransportSE(WEBSERVICEURL);
httpTransportSE.debug = true;
try {
httpTransportSE.call(SOAPACTION, soapEnvelope);
testObject1 soapResponse = (testObject1) soapEnvelope.bodyIn;//在这一句提示java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
} catch (Exception e) {
e.printStackTrace();
}
Webservice方法
public testObject1 HelloWorld()
{
testObject1 obj = new testObject1();
obj.testName1 = "return";
return obj;
}
Webservice返回的soap xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http-://www.w3.-org/2001/XMLSchema-instance" xmlns:xsd="http:-//www.w3.-org/2001/XMLSchema" xmlns:soap="http:-//schemas.xmlsoap.-org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http:-//tempuri.-org/">
<HelloWorldResult>
<testName1>string</testName1>
</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
使用 object 接收
Webservice一定要返回json类型的文本数据。