I try to write an Android application that connects to a .NET WCF web
service. When I tried to send primitive types, the server received
those values and sent a correct response. However, when I try to send
a complex type object, it cannot receive that object, it receives null
and sends a response accordingly. Here is the class definition of the
object that i want to send to the service.
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class TwoIntegerWrapper implements KvmSerializable {
private int Num1;
private int Num2;
public TwoIntegerWrapper() {
super();
}
TwoIntegerWrapper(int number1, int number2) {
super();
this.Num1 = number1;
this.Num2 = number2;
}
public int getNumber1() {
return Num1;
}
public void setNumber1(int number1) {
this.Num1 = number1;
}
public int getNumber2() {
return Num2;
}
public void setNumber2(int number2) {
this.Num2 = number2;
}
public Object getProperty(int propertyNumber) {
Object property = null;
switch (propertyNumber) {
case 0:
property = this.Num1;
break;
case 1:
property = this.Num2;
break;
}
return property;
}
public int getPropertyCount() {
return 2;
}
public void getPropertyInfo(int propertyNumber, Hashtable arg1,
PropertyInfo propertyInfo) {
switch (propertyNumber) {
case 0:
propertyInfo.type = PropertyInfo.INTEGER_CLASS;
propertyInfo.name = "Num1";
break;
case 1:
propertyInfo.type = PropertyInfo.INTEGER_CLASS;
propertyInfo.name = "Num2";
break;
}
}
public void setProperty(int propertyNumber, Object data) {
switch (propertyNumber) {
case 0:
this.Num1 = Integer.parseInt(data.toString());
break;
case 1:
this.Num2 = Integer.parseInt(data.toString());
break;
}
}
}
And here is how I call the web service.
SoapObject request = new SoapObject(NAMESPACE,
METHOD_NAME2);
PropertyInfo property = new PropertyInfo();
TwoIntegerWrapper tiw = new TwoIntegerWrapper(num1Value,
num2Value);
property.setName("TwoIntegerWrapper");
property.setType(tiw.getClass());
property.setValue(tiw);
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, tiw.getClass().getSimpleName(),
TwoIntegerWrapper.class);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(Add_SOAP_ACTION, envelope);
Log.d(logtag + " request dump", androidHttpTransport.requestDump);
Log.d(logtag + " response dump",
androidHttpTransport.responseDump);
SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
result = Integer.parseInt(sp.toString());
} catch (Exception e) {
Log.e(logtag, e.getMessage());
}
And request dump is the following:
<v:Envelope xmlns:i="
w3.org/2001/XMLSchema-instance" xmlns:d="
w3.org/
2001/XMLSchema" xmlns:c="
schemas.xmlsoap.org/soap/encoding/"
xmlns:v="
schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<AddNumbers2 xmlns="
tempuri.org/" id="o0" c:root="1">
<TwoIntegerWrapper i:type="n0:TwoIntegerWrapper"
xmlns:n0="
tempuri.org/">
<Num1 i:type="d:int">1</Num1>
<Num2 i:type="d:int">3</Num2>
</TwoIntegerWrapper>
</AddNumbers2>
</v:Body>
</v:Envelope>