Pass HttpClient object from one activity to another

632 views Asked by At

I have a class that use DefaultHttpClient object to execute http rest requests. Im trying to use the same HttpClient object to execute another request in different class and I tried to pass the class I'm using as a serialized object to the new activity. but when at runtime it gives a java.io.NotSerializableException for DefaultHttpClient is there a way I can pass DefaultHttpClient object as it is to the next Activity I'm loading.

09-03 12:51:44.891: E/AndroidRuntime(32297): FATAL EXCEPTION: main
09-03 12:51:44.891: E/AndroidRuntime(32297): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.hcp.hcpandroid.SSLAuthenticate)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Parcel.writeSerializable(Parcel.java:1279)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Parcel.writeValue(Parcel.java:1233)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Parcel.writeMapInternal(Parcel.java:591)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Bundle.writeToParcel(Bundle.java:1619)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Parcel.writeBundle(Parcel.java:605)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.content.Intent.writeToParcel(Intent.java:6519)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1741)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1411)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.Activity.startActivityForResult(Activity.java:3351)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.Activity.startActivityForResult(Activity.java:3312)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.Activity.startActivity(Activity.java:3522)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.Activity.startActivity(Activity.java:3490)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at com.hcp.hcpandroid.MainLogIn.hcpLogIn(MainLogIn.java:67)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at com.hcp.hcpandroid.MainLogIn$1.onClick(MainLogIn.java:35)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.view.View.performClick(View.java:4128)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.view.View$PerformClick.run(View.java:17142)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Handler.handleCallback(Handler.java:615)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Looper.loop(Looper.java:213)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.app.ActivityThread.main(ActivityThread.java:4787)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.lang.reflect.Method.invokeNative(Native Method)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.lang.reflect.Method.invoke(Method.java:511)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at dalvik.system.NativeStart.main(Native Method)
09-03 12:51:44.891: E/AndroidRuntime(32297): Caused by: java.io.NotSerializableException: org.apache.http.impl.client.DefaultHttpClient
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-03 12:51:44.891: E/AndroidRuntime(32297):    at android.os.Parcel.writeSerializable(Parcel.java:1274)
09-03 12:51:44.891: E/AndroidRuntime(32297):    ... 24 more
2

There are 2 answers

0
StErMi On

I think that the best thing to do is to use a static variable to store your Http client.

I would like to suggest you to take a look at OkHttp (from Square) or Ion (from Koush Dutta). Anyway I think that the real problem is that your object is not serializable and it's not parcellable for sure :)

So you cannot pass it through activies. The best think to do is to have a Service in background that handle your http requests and have only 1 (or how many you need) Http clients.

0
Antoine Marques On

In fact, even if you succeed to serialize you DefaultHttpClient, when you get it in the other Activity, you'll have a different instance.
I suggest you rather some proxy that will send commands from your second Activity to the original client, and receive responses from it.

What you need is some IPC mechanism to achieve this. You could try using :

  1. A Service if your Activities run in the same Process
  2. A ContentProvider if your Activities do not (have a look to aidl)
  3. android.os mechanism such as Handler, Message for IPC communication.

Please have a look at Android's Processes and Threads guide