Search code examples
androidwcfserializationksoap2

kSoap2 serialisation error


I'm using kSoap2 to connect to a WCF service from an android device but I get an serialisation error:

Cannot serialize: CompositeType : StringValue = Test, BoolValue = true

Here is the code for the webservice call:

public void call() {
    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        CompositeType comp = new CompositeType();
        comp.setProperty(CompositeType.STRING_VALUE, "Test");
        comp.setProperty(CompositeType.BOOLEAN_VALUE, true);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("CompositeType");
        pi.setValue(comp);
        pi.setType(comp.getClass());
        request.addProperty("composite", pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        envelope.addMapping(NAMESPACE, "CompositeType", (new CompositeType()).getClass());

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject) envelope.getResponse();

        comp.setProperty(CompositeType.BOOLEAN_VALUE, Boolean.parseBoolean(result.getProperty(0).toString()));
        comp.setProperty(CompositeType.STRING_VALUE, result.getProperty(1).toString());

        sb.append(comp.toString());
    } catch (Exception e) {
        e.printStackTrace();
        sb.append("Error:\n" + e.getMessage() + "\n");
    }

}

And this is my code for the CompositeType class:

public class CompositeType implements KvmSerializable{

public static final int STRING_VALUE = 1;
public static final int BOOLEAN_VALUE = 0;

public String StringValue;
public Boolean BoolValue;

public CompositeType() {
}

@Override
public String toString() {
    return "StringValue = " + StringValue + ", BoolValue = " + BoolValue.toString();
}

@Override
public Object getProperty(int arg0) {
    switch(arg0)
    {
    case STRING_VALUE:
        return StringValue;
    case BOOLEAN_VALUE:
        return BoolValue;
    }

    return null;
}

@Override
public int getPropertyCount() {
    return 2;
}

@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case STRING_VALUE:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "StringValue";
        break;
    case BOOLEAN_VALUE:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "BoolValue";
        break;
    default:break;
    }
}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case STRING_VALUE:
        StringValue = value.toString();
        break;
    case BOOLEAN_VALUE:
        BoolValue = Boolean.parseBoolean(value.toString());
        break;
    default:
        break;
    }
}


}

I thought objects that implement KvmSerializable can be serialized ... Does anyone know how to get it working?


Solution

  • This reply might be too little and too late, but I have noticed a problem with boolean params.

    I just changed the booleans to ints and my services worked fine! you will also need to set the web service params / objects to use int's and then convert to bools if you like!