Why is Android's DefaultHttpClient not restoring session as expected? - android

I am using Android's DefaultHttpClient to communicate with my server, I run into this weird problem.
This is basically what I want to achieve: When one of my activity is closed(onPause is called), I store all my Cookies into SharedPreferences. And when another activity is activated, I restore the Cookies from SharedPreferences. By doing so, I can persist session between activities.
I achieved this by calling httpClient.getCookieStore().getCookies() and store all their information into SharedPrefenrences in my Activity's onPause mehod.
I restore it by retrieving cookies from SharedPrefenrences and call httpClient.getCookieStore().addCookie().
I have logged all the progress and saw no difference between the cookies when I stored them and the cookies when I retrieved then from SharedPrefrence.
The problem is it's not working, the server still thinks I am not logged in(I have logged in before I cleared all cookies and restore then from file).

Related

Where to store the login state in Clean Architecture?

I am developing a login function in my Android application with a clean architecture.
And on other screens I need to observe the login state and do some logic according to the login state.
The question is where to store the login state? In the Domain layer or the Data layer?
You have to save it in the data layer, in the database or sharedPreferences
Check this image
Usually store the login state in the android Data layer(SharePreference or sqlite), every time when the login in or login out.
login in and login out may make an HTTP network request.
Store in the Data layer could reduce the number of HTTP network requests, and make activity which needs the login state launchs more faster.

How can I save cookies in internal storage and reuse them?

I've created an Android App with Web View that displays a web-site. Users have to login and my cookies lasts for 8 hours. I need to store the cookies into internal storage and then use that stored data to load the cookies again every time I start the app. Can someone help me ? I've checked lots of questions but I can't figure this out, I really need a code example for that. Thanks !
A CookieStore object represents a storage for cookie. Can store and retrieve cookies.
CookieManager will call CookieStore.add to save cookies for every incoming HTTP response, and call CookieStore.get to retrieve cookie for every outgoing HTTP request. A CookieStore is responsible for removing HttpCookie instances which have expired.
further information Click Hear: https://developer.android.com/reference/java/net/CookieStore.html

Session time-out in webservices

I am developing a Restful Webservice using Eclipse and an android application. User logs in using its username and password on the android application which is checked against the database at the server side(using this web service).
Now, I want to maintain a session for which the user will remain logged in, that is, the user does not have to log in again and again whenever he re-opens the application.
How to do it? I searched on net but I could not find an accurate solution. Kindly help with the appropriate solution. Ask if more information is required.
use Shared Preferences to save session data .
there are good tutorials :
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
http://androidexample.com/Android_Session_Management_Using_SharedPreferences_-_Android_Example/index.php?view=article_discription&aid=127
This can be done in two ways. One is storing them in a global variables and second is storing the data in shared preferences. The problem with storing data in global variable is data will be lost once user closes the application, but storing the data in shared preferences will be persistent even though user closes the application. Here is the complete example:
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

How to mantain the LTPA Token after closing the application?

I have implemented WASLTPAAuthentication (with WASLTPALoginModule and Realm) and it works well.
I can call my JAX-RS and they took the identity by the cookie.
My problem is: after I tap the Home button and open the menu of recent applications to swipe and quit the application, then once I open it again the session is destroyed and the cookie is lost and I need to insert my credentials and login again.
Is there a way to prevent this? Do I need to store the cookie on the localStorage in some way?
It looks like even though your LTPA cookie is still valid, when you close the application and the session dies, the cookies are being cleared. You will need to save the LTPA cookie in local storage and then set the cookie manually if you want it to use it for multiple sessions.
The LTPA cookie is included in the attributes object of the UserIdentity returned after successfully logging into the WASLTPARealm, so you should already have access to it. Its just a matter of saving and retrieving it.

How to store the cookie info when android killed my app

I have an app need login, I use a singleton http client to do everything, so it can track the cookies for me.
But when I launch a browser intent in my app to view some html pages, the app sometimes be killed by low memory, when user come back from the browser, my app activity would be recreated, but the new http client would not contains that login session id.
So I think what I need is to cache the cookies when my app get killed, and then restore it back when the app got recreated. I know there is a CookieSyncManager, but I do not have a full picture of how to use that.
(1) So How can I do that? is Cookie seralizable, I just thought to cache it in the sdcard, maybe a bad idea.
Another more general question maybe:
(2) How to share httpclient with webview/system browser? Not just pass cookies from httpclient to webiew/browser, but also get the cookies when initialize the cookies, How to make the http client and webview/browser share just ONE copy of cookie store in any time?
Use SharedPreferences to store cookies as string.
Take a look at this; http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
Also android developers use parcelable class, rather than serializable class. I think the reason is performance issues # mobile devices...

Categories

Resources