警告:无法序列化1.0

时间:2012-12-27 05:35:45

标签: android web-services soap wsdl android-ksoap2

我是android新手。我正在创建一个使用Web服务的计算器应用程序。 Web服务是SOAP。所以我包含一个带依赖关系的Ksoap2.jar文件。 我从编辑文本中获取输入,如双数据类型并将其传递给Web服务。 此Web服务调用还将参数作为double和调用函数添加从服务器添加和返回结果。

我的Android等号按钮调用web服务的代码是:

private void onEqualsButtonClick() {        
    secondNumber = Double.parseDouble(editTextInput.getText().toString());
    //secondNumber = Integer.parseInt(editTextInput.getText().toString());
    editTextInput.setText("");
    if(flag==1){            
        //Initialize soap request + add parameters
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

        //Use this to add parameters
        request.addProperty("number1",firstNumber);
        request.addProperty("number2",secondNumber);

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        try {
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
              androidHttpTransport.debug = true;
              //this is the actual part that will call the  
              androidHttpTransport.call(SOAP_ACTION, envelope);
              // Get the SoapResult from the envelope body.
              SoapObject result = (SoapObject)envelope.bodyIn;
              if(result != null){
                    //Get the first property and change the label text
                    editTextInput.setText(""+result.getProperty(0).toString());
              } else{
                    Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
              }
        } catch (Exception e) {
              e.printStackTrace();
        }

Web服务的wsdl是:

<definitions
 name="Calculate_WebService"
 targetNamespace="http://calculatorapplication.ocs.com/"
 xmlns="http://schemas.xmlsoap.org/wsdl/"
 xmlns:tns="http://calculatorapplication.xxx.com/"
 xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
 xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
>
<types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://calculatorapplication.ocs.com/"
         elementFormDefault="qualified" xmlns:tns="http://calculatorapplication.xxx.com/"
         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:soap11-enc="http://schemas.xmlsoap.org/soap/encoding/">
        <element name="getAddition" type="tns:getAddition"/>
        <complexType name="getAddition">
            <sequence>
                <element name="number1" type="double"/>
                <element name="number2" type="double"/>
            </sequence>
        </complexType>
        <element name="getAdditionResponse" type="tns:getAdditionResponse"/>
        <complexType name="getAdditionResponse">
            <sequence>
                <element name="return" type="double"/>
            </sequence>
        </complexType>
    </schema>
</types>
<message name="Calculate_WebService_getAddition">
    <part name="parameters" element="tns:getAddition"/>
</message>
<message name="Calculate_WebService_getAdditionResponse">
    <part name="parameters" element="tns:getAdditionResponse"/>
</message>
<portType name="Calculate_WebService">
    <operation name="getAddition">
        <input message="tns:Calculate_WebService_getAddition"/>
        <output message="tns:Calculate_WebService_getAdditionResponse"/>
    </operation>
</portType>
<binding name="Calculate_WebServiceSoapHttp" type="tns:Calculate_WebService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="getAddition">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="literal"/>
        </input>
        <output>
            <soap:body use="literal"/>
        </output>
    </operation>
</binding>
<service name="Calculate_WebService">
    <port name="Calculate_WebServiceSoapHttpPort" binding="tns:Calculate_WebServiceSoapHttp">
        <soap:address location="http://xxx.xxx.x.xxxx/Calculator_Application_WebServices-Calculator_Application-context-root/Calculate_WebServiceSoapHttpPort"/>
    </port>
</service>

我正在收到警告,输出是As:

12-27 10:40:22.221: W/System.err(331): java.lang.RuntimeException: Cannot serialize: 1.0
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:708)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:692)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:661)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:645)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:702)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:618)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:198)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.transport.Transport.createRequestData(Transport.java:111)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
12-27 10:40:22.241: W/System.err(331):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
12-27 10:40:22.241: W/System.err(331):  at com.ocs.calculatorapplication.Acivity.CalculatorActivity.onEqualsButtonClick(CalculatorActivity.java:189)
12-27 10:40:22.251: W/System.err(331):  at com.ocs.calculatorapplication.Acivity.CalculatorActivity.onClick(CalculatorActivity.java:122)
12-27 10:40:22.251: W/System.err(331):  at android.view.View.performClick(View.java:2485)
12-27 10:40:22.251: W/System.err(331):  at android.view.View$PerformClick.run(View.java:9080)
12-27 10:40:22.251: W/System.err(331):  at android.os.Handler.handleCallback(Handler.java:587)
12-27 10:40:22.251: W/System.err(331):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 10:40:22.251: W/System.err(331):  at android.os.Looper.loop(Looper.java:123)
12-27 10:40:22.251: W/System.err(331):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-27 10:40:22.261: W/System.err(331):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 10:40:22.261: W/System.err(331):  at java.lang.reflect.Method.invoke(Method.java:507)
12-27 10:40:22.271: W/System.err(331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-27 10:40:22.271: W/System.err(331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-27 10:40:22.271: W/System.err(331):  at dalvik.system.NativeStart.main(Native Method)

我应该在代码中做些什么更改。 请尽快建议我......

还有其他API方法吗?

1 个答案:

答案 0 :(得分:3)

是的,我解决了这个问题。 我使用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;

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

这个课程的实施如下:

//Marshal class.
MarshalDouble marshaldDouble = new MarshalDouble();
marshaldDouble.register(envelope);

希望这会对所有人有所帮助。