Here is current simple description my app. It uses some remote server API, which uses standart HTTP session. Login activity. It calls auth class, passing login and password.
public class Auth extends AsyncTask{
...
private DefaultHttpClient client = new DefaultHttpClient();
private HttpContext localContext = new BasicHttpContext();
private CookieStore cookieStore = new BasicCookieStore();
...
public void auth(String login, String password) {
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost request = new HttpPost(url);
...
}
protected void onPostExecute(Boolean result){
parent.loginresponse(result)
}
On successful auth, remote server creates standart HTTP session, sending me cookie, saved in CookiStore. After login, loginresponse starts main activity. There I wish to have one universal class for all API requests.
How do I make correct keeping alive HTTP session information, created after login, between all activities, and passing it to needed functions for corresponding API methods?
If you use a DI framework like Dagger, you can maintain a HttpContext
between activities and inject it wherever you like!
You can use a singleton class that would look something like this:
public class UserSession
{
private static UserSession sUserSession;
/*
The rest of your class declarations...
*/
public get(){
if (sUserSession == null)
{
sUserSession = new UserSession();
}
return sUserSession;
}
}
Once an instance of this class is initialized, it will stay in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With