bythestarz bythestarz - 4 months ago 41
reST (reStructuredText) Question

How to pass session id from one page to another in Android Studio?

Following is the response that i get for a succesfull login. If i logout the token here would change. What I want here is i want to pass token id to another page which has a different url. And each time the token id changes il have to change the token id in the url. How do i do this?? Please Help!!

{
"status": {
"message": " ",
"Code": 200
},
"Error_Code": 0,
"Error_Message ": "",
"Token": "vqcrk5wp8646th",
"User Details": {
"user_reg_id": "123",
"Name": "ABC",
}

Answer

Create a static constant class to store the token id it can be override when you want to.here how to use assign and use it in any where in application.

Class Constant{
public static String token_id=null;

}

And set your token after parsing

String token_id=json.getString("token");
Constant.token_id=token_id;

And Retrieve it when bind to url

String existing_token_id=Constant.token_id;

OR You can use shared preference to store token id

Use of SharedPreference

And set your token after parsing

String token_id=json.getString("token");
SharedPreference  pref= PreferenceManager.getDefaultSharedPreferences(Activity.this);

SharedPreferences.Editor editor = pref.edit();
            editor.putString("token_id", token_id);
            editor.commit();

And Retrieve it when bind to url

SharedPreference  pref= PreferenceManager.getDefaultSharedPreferences(Activity.this);

String existing_token_id=pref.getString("token_id",null);

Hope this will help you.let me know.