In android, How do i save response coming from php login script as shared preference?

我的梦境 提交于 2019-12-13 09:58:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!