Parcelable encountered IOException when sending in

2019-06-14 16:04发布

问题:

in the onCreate of activit-main i make an inistance of a class named XMPPConectio at the constructor of the XMMPPConnection i take inistance of the activity-main and therefor i execute an Asychtask. in the progressUpdate i call triger function of activity-main. i the triger function i want to send inistance of XMPPconnectio to other activity by intent.putExtra but i get the error

Parcelable encountered IOException writing serializable object

the aim of doing all these is to have connection (when it is connected) to other activity.

please give me some sample code thank you

XMPPConectio class which implements Serializable:

public class XMPPConnectio implements Serializable {


    XMPPTCPConnection connection;
    String connectionMessages="";
    connectionXMPP connectionXMPPAsynch;
    MainActivity mainActivity;


    public XMPPTCPConnection getXMPPConnectio ()
    {
        return connection;
    }

   public XMPPConnectio(MainActivity mainActivity)
    {
    this.mainActivity=mainActivity;
    try
    {
        connectionXMPPAsynch =new connectionXMPP();
        connectionXMPPAsynch.execute();
    }
    catch (Exception e)
    {
    }
    }

    class connectionXMPP extends AsyncTask<String,Void,String>
    {

        @Override
        protected String doInBackground(String... params) {


            connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
            .setServiceName("192.168.1.6")
            .setUsernameAndPassword("ehsan", "123")
            .setPort(9090)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setDebuggerEnabled(true)
            .setCompressionEnabled(false).build());
            connection.setUseStreamManagement(true);
            connection.addConnectionListener(new ConnectionListener()
            {

                @Override
                public void connected(XMPPConnection connection) {
                    Log.d("connected", "yes connected successfully : ");
                    publishProgress();
                }

                @Override
                public void authenticated(XMPPConnection connection, boolean resumed) {
                    Log.d("connected","yes authenticated successfully : ");
                }

                @Override
                public void connectionClosed() {
                    Log.d("connected","yes connectionClosed successfully : ");
                }

                @Override
                public void connectionClosedOnError(Exception e) {

                    Log.d("connected","yes connectionClosedOnError  : ");
                }

                @Override
                public void reconnectionSuccessful() {
                    Log.d("connected","yes reconnection successfully : ");
                }

                @Override
                public void reconnectingIn(int seconds) {
                    Log.d("connected","yes reconnectingIn  : ");
                }

                @Override
                public void reconnectionFailed(Exception e) {
                    Log.d("connected","yes reconnectionFailed  : ");
                }
            });

           connect();
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            mainActivity.triger();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.d("","onPostExecute");
        }
         private void connect()
        {
        try {

                connection.connect();

            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    }
}

the Activity which make instance of XMPPConectio and cause Asychtask execution

package passargad.ehsan;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;

public class MainActivity extends ActionBarActivity implements Serializable {


    private Socket socket;
    private String serverIpAddress = "192.168.1.6";
    XMPPConnectio xmppConnectio;
    XMPPTCPConnection connection;
    private static final int REDIRECTED_SERVERPORT = 6789;


    FastFood fastFood;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent(this,SocketService.class);
        startService(intent);

        fastFood =new FastFood(this);

        xmppConnectio=new XMPPConnectio(this);


    }

// this is the function which is called (when connection is done ) by //onProgressUpdate of Asychtask 

   public void triger()
    {


        try {
            Intent intent=  new Intent(this,chat.class);
            intent.putExtra("XMPP",xmppConnectio);
             startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}

this is the activity which i want to have XMPPConnectio in there but the execution never reach to this

package passargad.ehsan;

import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.IOException;


public class chat extends ActionBarActivity {
    XMPPConnectio xmppConnectio;
    XMPPTCPConnection connection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);


        xmppConnectio=(XMPPConnectio)getIntent().getSerializableExtra("XMPP");
        Log.d("","done");
 }
}

i tried to make the question crystal clear by giving all the code. as i said the goal is to have the connection in all activities when connection is connected. thank you .

回答1:

if i want to have connection object accessible in every activities one of the solutions is using bundle and send the bundle with intent. but the actual problem is not having bundle option in API level 10 (android 2.3). the other problem was ,not having the option to use serializing and putExtra for intent because XMPPTCPConnection does not implemet serializable. so i used the third option which was defining the object static.

so i changed the class XMPPConectio to this

 import android.app.Application;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.IBinder;
    import android.util.Log;

    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.ConnectionListener;
    import org.jivesoftware.smack.SmackException;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.chat.Chat;
    import org.jivesoftware.smack.chat.ChatManager;
    import org.jivesoftware.smack.chat.ChatManagerListener;
    import org.jivesoftware.smack.chat.ChatMessageListener;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.tcp.XMPPTCPConnection;
    import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

    import java.io.IOException;
    import java.io.Serializable;


    public class XMPPConnectio  {


        XMPPTCPConnection connection;
        String connectionMessages="";
        connectionXMPP connectionXMPPAsynch;
        MainActivity mainActivity;


        public XMPPTCPConnection getXMPPConnectio ()
        {
            return connection;
        }

       public XMPPConnectio(MainActivity mainActivity)
        {
        this.mainActivity=mainActivity;
        try
        {
            connectionXMPPAsynch =new connectionXMPP();
            connectionXMPPAsynch.execute();
        }
        catch (Exception e)
        {
        }
        }

        class connectionXMPP extends AsyncTask<String,Void,String>
        {

            @Override
            protected String doInBackground(String... params) {


                connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
                .setServiceName("192.168.1.6")
                .setUsernameAndPassword("ehsan", "123")
                .setPort(9090)
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setDebuggerEnabled(true)
                .setCompressionEnabled(false).build());
                connection.setUseStreamManagement(true);
                connection.addConnectionListener(new ConnectionListener()
                {

                    @Override
                    public void connected(XMPPConnection connection) {

                        Log.d("connected","yes connected successfully : ");
                        publishProgress();
                    }

                    @Override
                    public void authenticated(XMPPConnection connection, boolean resumed) {
                        Log.d("connected","yes authenticated successfully : ");

                    }

                    @Override
                    public void connectionClosed() {
                        Log.d("connected","yes connectionClosed successfully : ");
                    }

                    @Override
                    public void connectionClosedOnError(Exception e) {

                        Log.d("connected","yes connectionClosedOnError  : ");
                    }

                    @Override
                    public void reconnectionSuccessful() {
                        Log.d("connected","yes reconnection successfully : ");
                    }

                    @Override
                    public void reconnectingIn(int seconds) {
                        Log.d("connected","yes reconnectingIn  : ");
                    }

                    @Override
                    public void reconnectionFailed(Exception e) {
                        Log.d("connected","yes reconnectionFailed  : ");
                    }
                });

               connect();
                return null;
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
                mainActivity.triger();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Log.d("","onPostExecute");
            }
             private void connect()
            {
            try {

                    connection.connect();
                   // connection.login();

                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        }
    }

the Activity which make instance of XMPPConectio and cause Asychtask execution

package passargad.ehsan;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;

public class MainActivity extends ActionBarActivity {

private Socket socket;
private String serverIpAddress = "192.168.1.6";
XMPPConnectio xmppConnectio;
public static XMPPTCPConnection connection;
private static final int REDIRECTED_SERVERPORT = 6789;


FastFood fastFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent(this,SocketService.class);
    startService(intent);
    fastFood =new FastFood(this);

    //this is
    xmppConnectio=new XMPPConnectio(this);



}

public void triger()
{

    connection=xmppConnectio.getXMPPConnectio();
    Intent intent=  new Intent(this,chat.class);
    startActivity(intent);

}

}

this is the activity which we can access the connection there

package passargad.ehsan;

import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.IOException;


public class chat extends ActionBarActivity {
XMPPTCPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    //this is
        connection=MainActivity.connection;
        connection.addConnectionListener(new ConnectionListener() {
            @Override
            public void connected(XMPPConnection connection) {
                Log.d("","connected");

            }
        @Override
        public void authenticated(XMPPConnection connection, boolean resumed) {
            Log.d("","6");
        }

        @Override
        public void connectionClosed() {
            Log.d("","5");
        }

        @Override
        public void connectionClosedOnError(Exception e) {
            Log.d("","4");
        }

        @Override
        public void reconnectionSuccessful() {
            Log.d("","3");
        }

        @Override
        public void reconnectingIn(int seconds) {
            Log.d("","2");
        }

        @Override
        public void reconnectionFailed(Exception e) {
            Log.d("","1");
        }
    });
    try {
        connection.login();
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
}