Android的 - 如何双值发送到WS服务 [英] Android - How to send a double value to the ws service

查看:117
本文介绍了Android的 - 如何双值发送到WS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用KSOAP库双值发送到Web服务。
这是我试过,但它不工作。任何人都可以解释如何使这项工作。

 公共字符串getDataForStaticSearch()抛出的SOAPFault
{    字符串数据=;
    字符串的serviceUrl = RB_Constant.RB_Webservice_URL;
    字符串serviceNamespace = RB_Constant.RB_Webservice_Namespace;
    字符串的soapAction =htt​​p://www.roadbrake.com/GetSearchResultsV2;
    字符串type_of_soap =GetSearchResultsV2;    的PropertyInfo headingdirectionObj =新的PropertyInfo();
    headingdirectionObj.name =headingdirection;
    headingdirectionObj.type = PropertyInfo.INTEGER_CLASS;    尝试
    {
        SoapObject请求=新SoapObject(serviceNamespace,type_of_soap);        // strUserLatitude和strUserLongitude是双精度型。
        //如何将这些值传递给WS。
        Request.addProperty(strUserLatitude,33.924012);
        Request.addProperty(strUserLongitude,-118.3832772);         // headingdirectionObj的类型为int
        Request.addProperty(headingdirectionObj,0);        的System.out.println(请求价值 - >中+ Request.toString());        SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = TRUE;
        envelope.setOutputSoapObject(请求);        尝试
        {
            HttpTransportSE androidHttpTransport =新HttpTransportSE(的serviceUrl);
            androidHttpTransport.call(的soapAction,信封);
        }
        赶上(例外五)
        {
            的System.out.println(web服务调用错误 - >中+ e.toString());
        }        SoapPrimitive响应=(SoapPrimitive)envelope.getResponse();
        数据= response.toString();
        的System.out.println(web服务响应 - >中+ response.toString());
    }
    赶上(例外五)
    {
        的System.out.println(SOAP方法错误 - >中+ e.toString());
    }    返回的数据;
}


解决方案

要准确使用这样

Marshal类

 进口org.ksoap2.serialization.Marshal;
进口org.ksoap2.serialization.PropertyInfo;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.xmlpull.v1.XmlPullParser;
进口org.xmlpull.v1.XmlPullParserException;
进口org.xmlpull.v1.XmlSerializer;进口java.io.IOException异常;
公共类MarshalDouble实现元帅{
    公共对象readInstance(XmlPullParser分析器,字符串命名,字符串名称,
                               预期的PropertyInfo)抛出IOException异常,XmlPullParserException {        返回Double.parseDouble(parser.nextText());
    }
    公共无效寄存器(SoapSerializationEnvelope厘米){
        cm.addMapping(cm.xsd,双,Double.class,这一点);    }
    公共无效writeInstance(XmlSerializer的作家,obj对象)抛出IOException
        writer.text(obj.toString());
    }
}实施SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = TRUE;
envelope.dotNet = TRUE;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
envelope.setOutputSoapObject(请求);** MarshalDouble MD =新MarshalDouble();
md.register(信封)**

I am trying to send a double value to the web service using ksoap library. This is what I tried, but it doesn't work. Can anybody explain how to make this work.

public String getDataForStaticSearch() throws SoapFault   
{  

    String data = "";
    String serviceUrl = RB_Constant.RB_Webservice_URL;
    String serviceNamespace = RB_Constant.RB_Webservice_Namespace; 
    String soapAction = "http://www.roadbrake.com/GetSearchResultsV2";
    String type_of_soap = "GetSearchResultsV2";  

    PropertyInfo headingdirectionObj = new PropertyInfo ();
    headingdirectionObj.name = "headingdirection";
    headingdirectionObj.type = PropertyInfo.INTEGER_CLASS;  

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

        //  strUserLatitude and strUserLongitude are of type double.
        // How to pass these values to ws.  
        Request.addProperty("strUserLatitude", 33.924012);          
        Request.addProperty("strUserLongitude", -118.3832772);

         //headingdirectionObj is of type int
        Request.addProperty(headingdirectionObj, 0);

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

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        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;
}

解决方案

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

这篇关于Android的 - 如何双值发送到WS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆