android -如何使用 ksoap 将双值传递给服务

标签 android web-services ksoap

在我的应用程序中,我想使用ksoap双值传递给网络服务,但我得到了NPE。在代码中“exit_distance”是 double 值。任何人都可以找到其中的错误并发送示例示例

在此处编写代码

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

最佳答案

确切地说,像这样使用它

编码(marshal)级

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

关于android -如何使用 ksoap 将双值传递给服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6608701/

相关文章:

android - 如何使用 jMock ClassImposterizer 进行 Android 单元测试?

android - 在视频开始播放之前添加 "please wait loading...."

c# - 许多 WebReferences 使用相同的类

c# - 改进解析解决方案

android - KSOAP 2 复杂类

android - 如何配置 Eclipse 以在 Ubuntu 上使用 Phonegap?

java - 如何创建包装器来区分 2 个或更多独立 Web 服务应用程序

安卓 WSDL 网络服务 ksoap2

android - 如何将照片从安卓图库上传到服务器?

android - 在 Android 中找不到数组