Abutouq Abutouq - 19 days ago 11
Android Question

Android save user session

I'm new in android I'm trying to save login session and send it with every time I send request to server, My web service is PHP (Zend framework 2).

This code to identify user:

$auth = Zend_Auth::getInstance();
$IdentityObj = $auth->getIdentity();


It's possible save session and send it like I use web browser without make any change in my web service.

Answer

Yes, it is. You just have to store the session using SharedPreferences.

The idea is, after you login, I guess you return some kind of session id or whatever. You just need to store it. Then, in subsequents executions, you just fetch that session before doing any request.

SharedPreferences preferences = 
    getSharedPreferences("com.blabla.yourapp", Context.MODE_PRIVATE);

//Save it
preferences.edit().putString("session", <yoursessiontoken>).commit();

//Fetch it
// The second parameter is the default value.
preferences.getString("session", ""); 

Anyway is better that you read the documentation to really understand what you are doing.