ksoap2序列化错误“RuntimeException:无法序列化”

时间:2012-09-05 00:30:48

标签: android android-ksoap2

我正在尝试使用Android app调用Web服务。所有网络操作都在一个扩展AsyncTask类的单独类中完成。

package com.webservicedemo1;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WebServiceDemoActivity extends Activity

{
/** Called when the activity is first created. */

private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

Button btnFar, btnCel, btnClear;

EditText txtFar, txtCel;

@Override
public void onCreate(Bundle savedInstanceState)

{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_web_service_demo);

    btnFar = (Button) findViewById(R.id.button1);

    btnCel = (Button) findViewById(R.id.button2);

    btnClear = (Button) findViewById(R.id.button3);

    txtFar = (EditText) findViewById(R.id.editText1);

    txtCel = (EditText) findViewById(R.id.editText2);

    btnFar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v)

        {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
            request.addProperty("Fahrenheit", txtFar.getText().toString());
            BackGround bg = new BackGround();
            bg.execute(request);
            //request.getP
        }

    });

    btnCel.setOnClickListener(new View.OnClickListener()

    {

        @Override
        public void onClick(View v) {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
            request.addProperty("Celsius", txtCel.getText().toString());
            BackGround bg = new BackGround();
            bg.execute(request);

        }

    });

    btnClear.setOnClickListener(new View.OnClickListener()

    {

        @Override
        public void onClick(View v)

        {
            txtCel.setText("");
            txtFar.setText("");

        }

    });

}

private class BackGround extends AsyncTask<SoapObject, Object, SoapObject> {
    SoapObject result = null;

    @Override
    protected SoapObject doInBackground(SoapObject... req) {
        // TODO Auto-generated method stub
        // Initialize soap request + add parameters

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

        envelope.setOutputSoapObject(req);

        envelope.dotNet = true;

        try {


            PropertyInfo pi = new PropertyInfo();

            req[0].getPropertyInfo(0, pi);
            String type=pi.getName();


            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            if(type.equals("Fahrenheit")){
            // this is the actual part that will call the webservice
            androidHttpTransport.call(SOAP_ACTION1, envelope);
            }
            else if(type.equals("Celsius")){
                androidHttpTransport.call(SOAP_ACTION2, envelope);
            }
            // Get the SoapResult from the envelope body.
            result = (SoapObject) envelope.bodyIn;


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

    }

    @Override
    protected void onPostExecute(SoapObject res) {
        // TODO Auto-generated method stub
        super.onPostExecute(res);
        if (res != null) {

            // Get the first property and change the label text
            txtFar.setText(res.getProperty(0).toString());

        }

        else {

            Toast.makeText(getApplicationContext(), "No Response",
                    Toast.LENGTH_LONG).show();

        }
    }

}

}

但我得到以下错误。

08-27 17:48:42.980: W/System.err(1014): java.lang.RuntimeException: Cannot serialize:      [Lorg.ksoap2.serialization.SoapObject;@412102b0
08-27 17:48:42.980: W/System.err(1014):     at   org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:708)
08-27 17:48:42.980: W/System.err(1014):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:618)
08-27 17:48:42.990: W/System.err(1014):     at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:198)
08-27 17:48:42.990: W/System.err(1014):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:111)
08-27 17:48:42.990: W/System.err(1014):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
08-27 17:48:42.990: W/System.err(1014):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
08-27 17:48:42.990: W/System.err(1014):     at com.webservicedemo1.WebServiceDemoActivity$BackGround.doInBackground(WebServiceDemoActivity.java:128)
08-27 17:48:42.990: W/System.err(1014):     at com.webservicedemo1.WebServiceDemoActivity$BackGround.doInBackground(WebServiceDemoActivity.java:1)
08-27 17:48:43.000: W/System.err(1014):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-27 17:48:43.000: W/System.err(1014):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-27 17:48:43.000: W/System.err(1014):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-27 17:48:43.000: W/System.err(1014):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-27 17:48:43.000: W/System.err(1014):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-27 17:48:43.000: W/System.err(1014):     at >java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-27 17:48:43.000: W/System.err(1014):     at java.lang.Thread.run(Thread.java:856)

无法找出发生这种情况的原因。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

我猜您需要更改代码的一行

envelope.setOutputSoapObject(req);

envelope.setOutputSoapObject(req[0]);

详细了解AsyncTask:Developer-site