开发者

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 in that and send the sample example

code here

//valus required for test 
RB_Constant.RB_Webservice_URL = ?
RB_Constant.RB_Webservice_Namespace = ?

pu开发者_JAVA技巧blic String getExitsRestaurants() throws SoapFault   
{           
    String data = "";
    String serviceUrl = RB_Constant.RB_Webservice_URL;
    String serviceNamespace = RB_Constant.RB_Webservice_Namespace; 
    String soapAction = "http://www.roadbrake.com/GetExitDetailsExtn";
    String type_of_soap = "GetExitDetailsExtn";      

    try
    {
        SoapObject Request = new SoapObject(serviceNamespace, type_of_soap);

        PropertyInfo HighwayIdObj = new PropertyInfo ();
        HighwayIdObj.name = "HighwayId";
        HighwayIdObj.type = PropertyInfo.INTEGER_CLASS;

        Request.addProperty("ExitNo", 7);
        Request.addProperty(HighwayIdObj, highwayid);   
        Request.addProperty("HighwayName", 95); 
        Request.addProperty("exit_distance", 1.2);                      


        System.out.println("Request Value->"+Request.toString());

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

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

        envelope.setOutputSoapObject(Request);

        try
        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(serviceUrl);
            androidHttpTransport.call(soapAction, envelope);
        }
        catch(Exception e)
        {
            System.out.println("Webservice calling error ->"+e.toString());
        }

        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        data = response.toString();
        System.out.println("web service response->"+response.toString());   
    }
    catch(Exception e)
    {
        System.out.println("Soap Method Error ->"+e.toString());    
    }        
    return data;
}   

public class MarshalDouble implements Marshal 
{

    @Override
    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, exit_distance, Double.class, this);

    }

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


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);**


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;


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜