Android无法序列化错误

时间:2014-07-30 16:59:34

标签: java android serialization android-ksoap2 ksoap

我正在开发一个使用android调用Web服务的项目。我使用ksoap2。 我创建了一个自己的数据类型(只是为了尝试),它包含两个字符串变量。就像这样

public class MyType {
    String fName;
    String lName;

    public MyType(String s1,String s2){
        fName = s1;
        lName = s2;
    }
}

我在两端创建了这种数据类型。(Web服务端和android应用程序端)。我编写了一个程序来调用Web服务,然后使用我的数据类型连接给定的字符串。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    public final static String URL = "http://192.168.69.1:8080/WebApplication4/MyWebService?wsdl";
    public static final String NAMESPACE = "http://mywebservice.android.com/";
    public static final String SOAP_ACTION_PREFIX = "/";
    private static final String METHOD = "objectMethod";
    private TextView textView;

    MyType mt = new MyType("Upul","Tharanga");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.test);
        AsyncTaskRunner runner = new AsyncTaskRunner(mt);
        runner.execute();
    }

    private class AsyncTaskRunner extends AsyncTask<Integer, String, String> {

        private String resp;
        MyType a;

        public AsyncTaskRunner(MyType a){
            this.a = a;
        }

        @Override
        protected String doInBackground(Integer... params) {
            publishProgress("Loading contents..."); // Calls onProgressUpdate()
            try {
                // SoapEnvelop.VER11 is SOAP Version 1.1 constant
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                SoapObject request = new SoapObject(NAMESPACE, METHOD);

                PropertyInfo pi1=new PropertyInfo();
                pi1.setType(String.class);
                pi1.setName("parameter");
                pi1.setValue(a);
                request.addProperty(pi1);

                envelope.bodyOut = request;
                HttpTransportSE transport = new HttpTransportSE(URL);
                try {
                    transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
                //bodyIn is the body object received with this envelope
                if (envelope.bodyIn != null) {
                    //getProperty() Returns a specific property at a certain index.
                    //SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
                    //Object resultSOAP = (SoapPrimitive)((SoapObject) envelope.bodyIn).getProperty(0);
                    Object resultSOAP = (SoapPrimitive)((SoapObject) envelope.bodyIn).getProperty(0);
                    resp=resultSOAP.toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                resp = e.getMessage();
            }
            return resp;
        }

        /**
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            // In this example it is the return value from the web service
            textView.setText(result);
        }

        /**
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            // Things to be done before execution of long running operation. For
            // example showing ProgessDialog
        }
        /**
         * 
         * @see android.os.AsyncTask#onProgressUpdate(Progress[])
         */
        @Override
        protected void onProgressUpdate(String... text) {
            textView.setText(text[0]);
            // Things to be done while execution of long running operation is in
            // progress. For example updating ProgessDialog
        }
    }


}

我的Web服务所做的是将MyType参数作为输入并连接这两个给定的字符串并返回连接的字符串。 当我运行Android应用程序时,我收到一个错误(我认为是运行时错误),说无法序列化MyType。 有什么建议可以解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试实施可序列化

public class MyType implements Serializable {
    String fName;
    String lName;

    public MyType(String s1,String s2){
        fName = s1;
        lName = s2;
    }
}