Problem with sending Complex Types to a WCF Service using ksoap2

306 views
Skip to first unread message

Dominik Mohr

unread,
Jun 17, 2011, 7:03:06 AM6/17/11
to ksoap2-android
Hey guys,
I'm trying to connect to a simple WCF-Service using the ksoap2 lib but
having problems. Maybe you can help me.

The WCF-Service:

public class Service1 : IService1 {
public string GetData(int value) {
return string.Format("You entered: {0}", value);
}

public string HelloWorld() {
return "Hello World";
}

public CompositeType GetDataUsingDataContract(CompositeType
composite) {
if (composite == null) {
throw new ArgumentNullException("composite");
}
if (composite.BoolValue) {
composite.StringValue += "Suffix";
}
return composite;
}
}

WSDL: http://localhost:62588/Service1.svc?wsdl

The Java-Code for my android app:

public static final String SOAP_ACTION = "http://tempuri.org/IService1/
GetDataUsingDataContract";
public static final String NAMESPACE = "http://tempuri.org/";
public static final String URL = "http://10.0.2.2:62588/Service1.svc";

public CompositeType getDataUsingDataContract(CompositeType
composite)
throws Exception {
SoapObject _client = new SoapObject(NAMESPACE,
"GetDataUsingDataContract");
// PropertyInfo pi = new PropertyInfo();
// pi.setType(new CompositeTypeDTO().getClass());
// pi.setName("CompositeTypeDTO");
// pi.setNamespace("http://tempuri.org/");
// pi.setValue(composite);
// _client.addProperty(pi);
_client.addProperty("composite", composite);
SoapSerializationEnvelope _envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
_envelope.setOutputSoapObject(_client);
_envelope.dotNet = true;
_envelope.encodingStyle = SoapEnvelope.ENC;
_envelope.setAddAdornments(false);
_envelope.implicitTypes = false;
_envelope.addMapping(NAMESPACE, "CompositeTypeDTO", (new
CompositeTypeDTO()).getClass());
HttpTransportSE _ht = new HttpTransportSE(URL);
_ht.debug = true;
_ht.call(SOAP_ACTION, _envelope);
SoapObject _ret = (SoapObject) _envelope.getResponse();
int _len = _ret.getPropertyCount();
CompositeTypeDTO _returned = new CompositeTypeDTO();
for (int _i = 0; _i < _len; _i++) {
_returned.setProperty(_i, _ret.getProperty(_i));
}
return _returned;
}

and finally my CompositeType class which extends the SoapObject that
implements KvmSerializable:

public final class CompositeType extends SoapObject {
private java.lang.Boolean BoolValue;
private java.lang.Integer Id;
private java.lang.String StringValue;

public CompositeType() {
super("", "");
}
public void setBoolValue(java.lang.Boolean boolValue) {
this.BoolValue = boolValue;
}

public java.lang.Boolean getBoolValue() {
return this.BoolValue;
}

public void setId(java.lang.Integer id) {
this.Id = id;
}

public java.lang.Integer getId() {
return this.Id;
}

public void setStringValue(java.lang.String stringValue) {
this.StringValue = stringValue;
}

public java.lang.String getStringValue() {
return this.StringValue;
}

public int getPropertyCount() {
return 3;
}

public Object getProperty(int __index) {
switch(__index) {
case 0: return BoolValue;
case 1: return Id;
case 2: return StringValue;
}
return null;
}

public void setProperty(int __index, Object __obj) {
switch(__index) {
case 0: BoolValue = (java.lang.Boolean) __obj; break;
case 1: Id = (java.lang.Integer) __obj; break;
case 2: StringValue = (java.lang.String) __obj; break;
}
}

public void getPropertyInfo(int __index,
@SuppressWarnings("rawtypes") Hashtable __table, PropertyInfo __info)
{
switch(__index) {
case 0:
__info.name = "BoolValue";
__info.type = java.lang.Boolean.class; break;
case 1:
__info.name = "Id";
__info.type = java.lang.Integer.class; break;
case 2:
__info.name = "StringValue";
__info.type = java.lang.String.class; break;
}
}
}

I can connect to the HelloWorld, and the GetData Services which take
none or one parameter but i can't connect to the service which takes a
complex type as input. If i use the code as is, i get the following
error:

SoapFault - faultcode: 'a:DeserializationFailed' faultstring: 'Der
Formatierer hat beim Deserialisieren der Nachricht eine Ausnahme
ausgelöst: Fehler beim Deserialisieren von Parameter http://tempuri.org/:composite.
Die InnerException-Nachricht war "Fehler in Zeile 1, Position 308. Das
Element "http://tempuri.org/:composite" enthält Daten eines Typs, der
dem Namen "http://tempuri.org/:" zugeordnet ist. Dem
Deserialisierungsprogramm ist kein Typ bekannt, der diesem Namen
zugeordnet ist. Verwenden Sie ggf. einen DataContractResolver, oder
fügen Sie den entsprechenden Typ für "" der Liste der bekannten Typen
hinzu. Verwenden Sie dazu z. B. das Attribut "KnownTypeAttribute",
oder fügen Sie den Typ der an DataContractSerializer übergebenen Liste
von bekannten Typen hinzu.". Weitere Details finden Sie unter
"InnerException".' faultactor: 'null' detail: null

I get the same error if i try to add the complex type with the
PropertyInfo, the code that is commented at the top. If i use
_client.addProperty("composite", pi) instead of
_client.addProperty(pi) i get the following error:

Cannot serialize: CompositeType

Maybe one of you can help me because i tried many many things to get
it working.

Dominik Mohr

Dominik Mohr

unread,
Jun 17, 2011, 7:14:22 AM6/17/11
to ksoap2-android
In addition, here is the soap request, generated by ksoap:

<v:Envelope
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<n0:GetDataUsingDataContract id="o0" c:root="1" xmlns:n0="http://
tempuri.org/">
<composite i:type=":">
<boolValue i:type="d:boolean">true</boolValue>
<id i:null="true" />
<stringValue i:type="d:string">Test</stringValue>
</composite>
</n0:GetDataUsingDataContract>
</v:Body>
</v:Envelope>

and this is the soap request which is generated by the Visual Studio
WCF Test Client:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/
ws/2005/05/addressing/none">http://tempuri.org/IService1/
GetDataUsingDataContract</Action>
</s:Header>
<s:Body>
<GetDataUsingDataContract xmlns="http://tempuri.org/">
<composite xmlns:d4p1="http://schemas.datacontract.org/2004/07/
DTO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:BoolValue>true</d4p1:BoolValue>
<d4p1:Id>0</d4p1:Id>
<d4p1:StringValue>Test</d4p1:StringValue>
</composite>
</GetDataUsingDataContract>
</s:Body>
</s:Envelope>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages