Reputation: 153
How should I handle session keys in my android application?
The client will receive a key and serial number from the server once they've logged in, the serial number will be changed every time the client makes a new request. The key will remain the same until it expires or is destroyed. These will need to be sent in every https request.
Here's an example of what the key and serial number looks like
key: af9bce267343ad72bd6abe7aff58edf2
S/N: 503:960:819
Should I use a SQLIte database even though there will be an absolute maximum of 1 session.
Upvotes: 3
Views: 1178
Reputation: 171
A SQLite database would be using a sledgehammer to crack a nut. I would store the information in the app's SharedPreferences
:
String myKey = "af9bce267343ad72bd6abe7aff58edf2";
String mySerial = "503:960:819";
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
// Set/Store data
edit.putString("session_key", myKey);
edit.putString("serial", mySerial);
// Commit the changes
edit.commit();
Whenever you want to retrieve the values again, use:
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
String myKey = pref.getString("session_key", null);
String mySerial = pref.getString("serial", null);
Upvotes: 2
Reputation: 5339
you can use SharedPreferences
, save the value like this
SharedPreferences sharedpreferences = getSharedPreferences("mPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("token", mToken);
editor.commit();
and retrieve it like this
sharedpreferences = getSharedPreferences("mPref", Context.MODE_PRIVATE);
String token = sharedpreferences.getString("token", null);
Upvotes: 1