调用SOAP WEB-Service时获取序列化异常

时间:2015-04-10 05:15:39

标签: android web-services post serialization

我在

处收到序列化错误
androidHttpTransport.call(SOAP_ACTION, envelope);

这是 WebService Class

的完整代码
public class WebserviceCall extends AsyncTask<String, Void, Void> {


    String result = "";

    HttpTransportSE androidHttpTransport;

    public WebserviceCall() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected Void doInBackground(String... urls) {

        try {
            SOAP_ACTION = namespace + METHOD_NAME;

            // Adding values to request object
            request = new SoapObject(namespace, METHOD_NAME);

            // Adding Double value to request object
            PropertyInfo weightProp = new PropertyInfo();
            weightProp.setName("Weight");
            double w = Double.parseDouble(urls[0]);
            weightProp.setValue(w);
            weightProp.setType(double.class);
            request.addProperty(weightProp);

            // Adding String value to request object
            request.addProperty("FromUnit", "" + urls[1]);
            request.addProperty("ToUnit", "" + urls[2]);

            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            // You can comment that line if your web service is not .NET
            // one.
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
            androidHttpTransport.debug = true;

            try {

                // SOAP calling webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                // Got Webservice response
                result = envelope.getResponse().toString();

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void r) {
        setResult(result);
    }
}

Logcat错误

> 04-10 10:25:05.612: W/System.err(919): java.lang.RuntimeException: Cannot serialize: 20.0
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:708)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:692)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:661)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:645)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:702)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:618)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:198)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.transport.Transport.createRequestData(Transport.java:111)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
04-10 10:25:05.612: W/System.err(919):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
04-10 10:25:05.622: W/System.err(919):  at com.postwebservicing.WebServicing$WebserviceCall.doInBackground(WebServicing.java:121)

2 个答案:

答案 0 :(得分:0)

试试这个, 拨打电话后,请添加以下行:

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();    
String resString=response.toString();

答案 1 :(得分:0)

没有必要将权重解析为double。我对我的代码进行了以下更改,现在它正在运行。

PropertyInfo weightProp =new PropertyInfo();
        weightProp.setName("Weight");
        weightProp.setValue(urls[0]);
        weightProp.setType(double.class);
        request.addProperty(weightProp);