1

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

我的 android equal button 调用 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>

我收到警告,输出为:

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 方法吗?

4

1 回答 1

3

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

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

希望这对大家有很大帮助。

于 2012-12-28T04:58:18.000 回答