vigamage
vigamage

Reputation: 2113

Android cannot serialize error

I am working on a project to invoke a web service using android. I use ksoap2 for that. I created a my own data type(just to try) which contains two string variables. It is like this

public class MyType {
    String fName;
    String lName;

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

I created this data type at both ends.(web service end and android application end). I wrote a program to invoke web service and then to concatenate given strings using my data type.

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


}

What my web service do is take the MyType parameter as input and concatenate those two given strings and return the concatenated string. When I run the android application I get an error(run time error I think) saying cannot serialize MyType. Any suggestions to solve the issue?

Upvotes: 0

Views: 1032

Answers (1)

mt523
mt523

Reputation: 88

Try implementing Serializable

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

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

Upvotes: 1

Related Questions