将特定对象传递给其他活动(Jsch)

时间:2014-03-03 13:42:56

标签: java android serialization network-programming parcelable

将Object从一个活动传递到另一个活动时遇到问题。 我正在使用Jsch()类,因为我想进行Sftp连接。

这在我的登录活动中:

ServerConnect lLogin = new ServerConnect(lUser,lPass,lIP,lPort);
ASYNCLogin lASYNC = new ASYNCLogin(lLogin,getBaseContext());
lASYNC.execute();   

我的ASYNC任务:

    @Override
    protected Integer doInBackground(Void... params)
    {
        if(mServerConnect.connectServer())
            return 1;       
        return 0;       
    }
    @Override
    protected void onPostExecute(Integer result)
    {       
        //Toast.makeText(mContext, "Sucessfully Connected!", Toast.LENGTH_LONG).show();         
        Intent lListFiles = new Intent(mContext,ListFilesActivity.class);       
        lListFiles.putExtra("ServerConnect", mServerConnect);
        lListFiles.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(lListFiles);
    }

我的ServerConnect类中的connect方法:

public boolean connectServer()
    {
        try
        {
            mSession=mSsh.getSession(mUsername,mServerIP,mServerPort);
            mSession.setConfig(mConfig);
            mSession.setPassword(mPassword);
            mSession.connect();         
            mChannel = mSession.openChannel("sftp");
            mChannel.connect();
        }
}

我如何在其他活动中获取对象:

mLogin = (ServerConnect) getIntent().getSerializableExtra("ServerConnect"); 

我尝试使用Serializable,但它不起作用,因为我无法在java.lang.Object类中实现Serializable接口.... 我也尝试使用Parcelable,但我不知道如何将自定义类写入Parcable。 是否有其他方法可以将Object从Login Activity传递给另一个?

logcat的:

03-03 13:35:40.583: E/AndroidRuntime(4186): FATAL EXCEPTION: main
03-03 13:35:40.583: E/AndroidRuntime(4186): Process: com.example.securebox, PID: 4186
03-03 13:35:40.583: E/AndroidRuntime(4186): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.securebox.ServerConnect)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Parcel.writeSerializable(Parcel.java:1312)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Parcel.writeValue(Parcel.java:1260)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Bundle.writeToParcel(Bundle.java:1665)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Parcel.writeBundle(Parcel.java:632)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.content.Intent.writeToParcel(Intent.java:7013)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2076)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.app.ContextImpl.startActivity(ContextImpl.java:1039)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.app.ContextImpl.startActivity(ContextImpl.java:1021)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at com.example.securebox.ASYNCLogin.onPostExecute(ASYNCLogin.java:32)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at com.example.securebox.ASYNCLogin.onPostExecute(ASYNCLogin.java:1)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Looper.loop(Looper.java:137)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.lang.reflect.Method.invoke(Method.java:515)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at dalvik.system.NativeStart.main(Native Method)
03-03 13:35:40.583: E/AndroidRuntime(4186): Caused by: java.io.NotSerializableException: java.lang.Object
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-03 13:35:40.583: E/AndroidRuntime(4186):     at android.os.Parcel.writeSerializable(Parcel.java:1307)
03-03 13:35:40.583: E/AndroidRuntime(4186):     ... 22 more

0 个答案:

没有答案