Skip to content Skip to sidebar Skip to footer

Android -How To Pass Double Value To Service Using Ksoap

In My application i want to pass the double value to the web service using ksoap but i am getting the NPE. In the code 'exit_distance' is double value. can any body find the error

Solution 1:

To be exact use it like this

the Marshal class

import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;


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());
    }
}

the implementation

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
envelope.setOutputSoapObject(request);

**MarshalDouble md = new MarshalDouble();
md.register(envelope);**

Solution 2:

you can simply set the type of the propertyInfo to be Double.class

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi=new PropertyInfo();

    pi.setName("name");
    pi.setValue(a);
    pi.setType(a.getClass());
    request.addProperty(pi);

    pi=new PropertyInfo();
    pi.setName("latitude");
    pi.setValue(b);
    pi.setType(Double.class);
    request.addProperty(pi);

    pi=new PropertyInfo();
    pi.setName("longitude");
    pi.setValue(c);
    pi.setType(Double.class);

    request.addProperty(pi);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
            envelope.dotNet = true;

Solution 3:

The stack trace with the NPE should give you the line number where it happens. Just set a debug break point there and see for yourself what is null.


Solution 4:

Finally i solved the issue. The problem is in register() method of MarshalDouble class. The Following one is the Solution of that problem.

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

Post a Comment for "Android -How To Pass Double Value To Service Using Ksoap"