问题
i have seen most of the examples of offline apps , but what should i do if my response comes from php api ? Basically my app logins and give request to an api , and i get a response. Suppose i get reponse as email and a token. So how should i use it in session management? so that user don't have to login all the time when he exits app.
回答1:
you can store email and token in shared prefrence like this
when user succefully login store data in sharedpreferences like this
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String email = "email";
public static final String token = "token";
public static final boolean isLogin = "islogin";
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// store login data in sharedpreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(email, "rathodnilsrk@gmail.com");
editor.putString(token, "123456789");
editor.putBoolean(isLogin, true);
editor.commit(); //save data in sharedpreferences
now when your application start that check that user is login or not like this
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// get boolean from sharedprefrence
public static final boolean isLogin = "islogin";
boolean login = prefs.getBoolean(isLogin, false);
// check login status
if(login){
// user session available move to home screen
}else{
// user does not login move to login screen
}
来源:https://stackoverflow.com/questions/45168556/in-android-how-do-i-save-response-coming-from-php-login-script-as-shared-prefer