在android中,如何将来自php登录脚本的响应保存为共享首选项?

在android中,如何将来自php登录脚本的响应保存为共享首选项?,android,sharedpreferences,session-management,Android,Sharedpreferences,Session Management,我已经看到了大多数离线应用程序的示例,但是如果我的响应来自php api,我应该怎么做?基本上,我的应用程序登录并向api发出请求,然后我得到响应。假设我收到电子邮件和令牌的回复。那么我应该如何在会话管理中使用它呢?这样用户在退出应用程序时就不必一直登录 您可以像这样在共享优先级中存储电子邮件和令牌 public static final String MyPREFERENCES = "MyPrefs" ; public static final String email = "email";

我已经看到了大多数离线应用程序的示例,但是如果我的响应来自php api,我应该怎么做?基本上,我的应用程序登录并向api发出请求,然后我得到响应。假设我收到电子邮件和令牌的回复。那么我应该如何在会话管理中使用它呢?这样用户在退出应用程序时就不必一直登录

您可以像这样在共享优先级中存储电子邮件和令牌

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
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
}
当用户成功登录时,将数据存储在如下SharedReference中

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
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
}
现在,当您的应用程序启动时,请检查用户是否登录

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
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
}

您可以使用SharedReferences保存电子邮件和令牌。如果您想知道如何做,请在Google中搜索您需要的内容您是否已经将响应正文作为字符串对象?@Roran是的,我正在获取响应并将其存储在字符串变量中,事实上我正在欢迎屏幕中使用它,但我想在维护会话时使用这些响应。谢谢,我将尝试以下操作:)对于注销按钮,我必须清除并提交编辑器,然后再次跳转到登录活动,对吗?顺便说一句,我可以把字符串变量放在像editor.putString(email,email)?而不是直接给予价值rathodnil@gmail.com正如你所给予的?因为我的值会随着用户的不同而变化(这很明显)。