Mohammad Rababah
Mohammad Rababah

Reputation: 1726

Ksoap2 in android cannot serialize

I use ksoap2 in android

to send list of numbers as string

But it have error:

java.lang.runtimeexception cannot serialize

I search a solution to this error but result not change

Can Help me please

public String Send(ArrayList<String> contactlist)
{
    try{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
            pi.setType(String.class);
            pi.setName("contactlist");
            pi.setValue(contactlist);
            request.addProperty("contactlist", pi);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        // Creating SOAP envelope           
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        new MarshalBase64().register(envelope); // this line is for serialization

        //You can comment that line if your web service is not .NET one.
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport("http://10.0.2.2:54869/Service1.asmx");
        androidHttpTransport.debug = true;
}
    catch (Exception exception)
        {
            return exception.toString();
        }
    try
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            //String result = envelope.getResponse().toString();
            return "";//result;
        }
    catch (Exception exception)
        {
            return exception.toString();
        }

Upvotes: 0

Views: 7157

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 8939

Use this class for Serialization

public class MarshalDouble implements Marshal {
        public Object readInstance(XmlPullParser parser, String namespace,
                String name, PropertyInfo expected) throws IOException,
                XmlPullParserException {

            return Double.parseDouble(parser.nextText());
        }

        public void register(SoapSerializationEnvelope cm) {
            cm.addMapping(cm.xsd, "double", Double.class, this);

        }

        public void writeInstance(XmlSerializer writer, Object obj)
                throws IOException {
            writer.text(obj.toString());
        }
    }

Add this line to envelop

    envelope.dotNet = true;
    envelope.implicitTypes = true;
    envelope.encodingStyle = SoapSerializationEnvelope.XSD;
    MarshalDouble md = new MarshalDouble();
    md.register(envelope);

Upvotes: 5

Related Questions