2

在我的应用程序中,我想将 传递double valueweb serviceusingksoap但我得到NPE. 在代码中“exit_distance”是双值。任何人都可以在其中找到错误并发送示例

代码在这里

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

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

4 回答 4

2

确切地说,像这样使用它

元帅级

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

实施

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);**
于 2012-07-30T09:40:31.453 回答
1

您可以简单地将 propertyInfo 的类型设置为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;
于 2012-10-09T05:12:33.667 回答
0

NPE 的堆栈跟踪应该为您提供它发生的行号。只需在那里设置一个调试断点,然后自己看看什么是空的。

于 2011-08-10T04:20:20.830 回答
0

最后我解决了这个问题。问题出在 MarshalDouble 类的 register() 方法中。以下是该问题的解决方案。

public void register(SoapSerializationEnvelope cm)  {
  cm.addMapping(cm.xsd, "double", Double.class, this);          
}
于 2012-06-07T10:58:03.110 回答