Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Parcelable
    text
    copied!<p>I have a class that controls my http communications, as well as keeps track of the http session. The problem i'm running into is, i would like to use this class from several activities.<br> The only option i have found so far is making it static in the main view, but if the app sits to long in a secondary view, the static class becomes null. I have also tried making it parcelable, but i can't pass my CookieStore or my httpContext. This means every time I go into a view, or out of a view, i have to re-authenticate against the server, causing much unnecessary traffic.</p> <p>How can i pass an object via Parcelable, or is there another type of class extension that would allow me to create a persistent class that all Activity's can view?</p> <pre><code>public class JSON implements Parcelable { private String URL; private String HOSTNAME; private Context f_context; private DefaultHttpClient httpClient; private CookieStore cookieStore; private HttpContext httpContext; protected boolean Authenticated = false; protected long Authenticate_timeout = 0; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { Bundle b = new Bundle(); b.putString("URL",URL); b.putString("HOSTNAME", HOSTNAME); b.putBoolean("Authenticated", Authenticated); b.putLong("Authenticate_timeout", Authenticate_timeout); out.writeBundle(b); Object[] o = new Object[4]; o[0] = httpClient; o[1] = cookieStore; o[2] = httpContext; o[3] = f_context; out.writeArray(o); } public static final Parcelable.Creator&lt;JSON&gt; CREATOR = new Parcelable.Creator&lt;JSON&gt;() { public JSON createFromParcel(Parcel in) { return new JSON(in); } public JSON[] newArray(int size) { return new JSON[size]; } }; private JSON(Parcel in) { Object[] o = in.readArray(null); httpClient = (DefaultHttpClient) o[0]; cookieStore = (CookieStore) o[1]; httpContext = (HttpContext) o[2]; f_context = (Context) o[3]; Bundle b = in.readBundle(); URL = b.getString("URL"); HOSTNAME = b.getString("HOSTNAME"); Authenticated = b.getBoolean("Authenticated"); Authenticate_timeout = b.getLong("Authenticate_timeout"); } public JSON(Context context) { f_context = context; updateSettings(); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams .setConnectionTimeout(myParams, Consts.http_timeout); HttpConnectionParams.setSoTimeout(myParams, Consts.http_timeout); httpClient = new DefaultHttpClient(myParams); cookieStore = new BasicCookieStore(); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload