Search code examples
androidweb-servicessoapwsdlandroid-ksoap2

Warning : cannot serialize 1.0


I am new to android. I am creating a calculator application which use web service. The web service is SOAP. so i include a Ksoap2.jar file with dependencies. I am taking input from edit text as in double data type and passing it to web service. This web service call is also taking parameters as double and call function addition from server which done addition and return result.

My code of android equal button calling web service is :

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

The wsdl of web service is :

<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>

And i am getting warning with out output is 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)

What changes should i do in code. Please suggest me as soon as possible...

Is there any another API approach?


Solution

  • Yes i solves this issue. I used the Marshal Class to solve this issue. The code of marshal class is as follows:

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

    And the implimentation of this class is as follows:

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

    Hope this will help a lot to all.