我想在android中使用ksoap2将一个类作为参数发送到Web服务。 这是我在android中的代码:
public Object simpleConnect() {
SoapObject request = new SoapObject(namespace, methodName);
Foo para = new Foo();
para.setFooId(2333222);
para.setCompanyId(12);
para.setField2(true);
para.setField3(2);
para.setPrimaryKey(765);
para.setUserId(987);
request.addProperty("param", para);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.addMapping(namespace, "Foo", Foo.class);
HttpTransportSE ht = new HttpTransportSE(url);
try {
ht.call(soapAction, envelope);
return envelope.getResponse(); //got error deserialize in this line
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这是我尝试调用它的服务方法:
public Foo addObjFoo(Foo param) throws SystemException {
System.out.println("something");
fooPersistence.update(param, false);
return param;
}
这是我的wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:http.service.test.com" xmlns:intf="urn:http.service.test.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://model.test.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:http.service.test.com">
<!--WSDL created by Apache Axis version: 1.4Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://model.test.com">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexType name="FooSoap">
<sequence>
<element name="companyId" type="xsd:long"/>
<element name="field2" type="xsd:boolean"/>
<element name="field3" type="xsd:int"/>
<element name="fooId" type="xsd:long"/>
<element name="primaryKey" type="xsd:long"/>
<element name="userId" type="xsd:long"/>
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="addFooResponse">
<wsdl:part name="addFooReturn" type="xsd:int"/>
</wsdl:message>
<wsdl:message name="printmessageRequest">
<wsdl:part name="behi" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="addFooRequest">
<wsdl:part name="para" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="printmessageResponse">
<wsdl:part name="printmessageReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="addObjFooResponse">
<wsdl:part name="addObjFooReturn" type="tns1:FooSoap"/>
</wsdl:message>
<wsdl:message name="addObjFooRequest">
<wsdl:part name="param" type="xsd:anyType"/>
</wsdl:message>
<wsdl:portType name="FooServiceSoap">
<wsdl:operation name="addFoo" parameterOrder="para">
<wsdl:input message="impl:addFooRequest" name="addFooRequest"/>
<wsdl:output message="impl:addFooResponse" name="addFooResponse"/>
</wsdl:operation>
<wsdl:operation name="addObjFoo" parameterOrder="param">
<wsdl:input message="impl:addObjFooRequest" name="addObjFooRequest"/>
<wsdl:output message="impl:addObjFooResponse" name="addObjFooResponse"/>
</wsdl:operation>
<wsdl:operation name="printmessage" parameterOrder="behi">
<wsdl:input message="impl:printmessageRequest" name="printmessageRequest"/>
<wsdl:output message="impl:printmessageResponse" name="printmessageResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Plugin_obtice_FooServiceSoapBinding" type="impl:FooServiceSoap">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="addFoo">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="addFooRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="addFooResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addObjFoo">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="addObjFooRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="addObjFooResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="printmessage">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="printmessageRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="printmessageResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:http.service.test.com" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FooServiceSoapService">
<wsdl:port binding="impl:Plugin_obtice_FooServiceSoapBinding" name="Plugin_obtice_FooService">
<wsdlsoap:address location="http://localhost:8080/WebService-portlet/axis/Plugin_obtice_FooService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
最后这是我在客户端的Foo类:
package com.obtice.liferaytest;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class Foo implements KvmSerializable {
protected long localCompanyId;
public long getCompanyId() {
return localCompanyId;
}
public void setCompanyId(long param) {
this.localCompanyId = param;
}
protected boolean localField2;
public boolean getField2() {
return localField2;
}
public void setField2(boolean param) {
this.localField2 = param;
}
protected int localField3;
public int getField3() {
return localField3;
}
public void setField3(int param) {
this.localField3 = param;
}
protected long localFooId;
public long getFooId() {
return localFooId;
}
public void setFooId(long param) {
this.localFooId = param;
}
protected long localPrimaryKey;
public long getPrimaryKey() {
return localPrimaryKey;
}
public void setPrimaryKey(long param) {
this.localPrimaryKey = param;
}
protected long localUserId;
public long getUserId() {
return localUserId;
}
public void setUserId(long param) {
this.localUserId = param;
}
@Override
public Object getProperty(int index) {
Object object = null;
switch (index) {
case 0:
object = localCompanyId;
break;
case 1:
object = localField2;
break;
case 2:
object = localField3;
break;
case 3:
object = localFooId;
break;
case 4:
object = localPrimaryKey;
break;
case 5:
object = localUserId;
break;
default:
break;
}
return object;
}
@Override
public int getPropertyCount() {
return 9;
}
@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch (index) {
case 0:
info.name = "companyId" ;
info.type = PropertyInfo.LONG_CLASS ;
break;
case 1:
info.name = "field2";
info.type = PropertyInfo.BOOLEAN_CLASS ;
break;
case 2:
info.name = "field3";
info.type = PropertyInfo.INTEGER_CLASS ;
break;
case 3:
info.name = "fooId";
info.type = PropertyInfo.LONG_CLASS ;
break;
case 4:
info.name = "primaryKey";
info.type = PropertyInfo.LONG_CLASS ;
break;
case 5:
info.name = "userId";
info.type = PropertyInfo.LONG_CLASS ;
break;
default:
break;
}
}
@Override
public void setProperty(int index, Object obj) {
String objStr = obj.toString();
switch (index) {
case 0:
localCompanyId = Long.parseLong(objStr);
break;
case 1:
localField2 = Boolean.parseBoolean(objStr);
break;
case 2:
localField3 = Integer.parseInt(objStr);
break;
case 3:
localFooId = Long.parseLong(objStr);
break;
case 4:
localPrimaryKey = Long.parseLong(objStr);
break;
case 5:
localUserId = Long.parseLong(objStr);
break;
default:
break;
}
}
}
我在调用Web服务和响应时收到此错误:
SoapFault - faultcode: 'soapenv:Server.userException' faultstring: 'org.xml.sax.SAXException: Deserializing parameter 'param': could not find deserializer for type {urn:http.service.test.com/}Foo' faultactor: 'null' detail: org.kxml2.kdom.Node@4057bb40
请帮帮我。我完全坚持了它。
谢谢......
答案 0 :(得分:1)
我收到了同样的错误消息,我疯狂地想弄明白。
在传输呼叫之前添加此行
androidHttpTransport.debug = true;
和这两个电话之后
Log.i(REQUEST, androidHttpTransport.requestDump);
Log.i(RESPONSE, androidHttpTransport.responseDump);
并查看您在信封中发送的Foo类型,并确保它与WSDL中声明的类型相匹配。
在我的项目中,结果是信封映射
envelope.addMapping(namespace, "GetFacilityReservations",GetFacilityReservations.class);
将我的自定义类发送为类型n0,因为这是我最初声明的命名空间的类型。但我的班级是另一种类型。
因此,我需要做的就是在我的映射中将名称空间更改为正确的名称。
envelope.addMapping("urn:XPressLocate", "GetFacilityReservations",GetFacilityReservations.class);