【发布时间】:2016-10-20 02:41:10
【问题描述】:
我的 LoginActivity 正在将用户名和密码发送到 URL 并获得成功状态。我想在会话中存储 json 响应,如用户 ID、姓名、电子邮件信息,并在 MainActivity 中检索它。我正在使用以下代码进行 json 响应。
JSONObject jObj = new JSONObject(response);
String userId = jObj.getString("user_id");
我的回答是
{"tag":"login",
"error":false,
"user_id":"5",
"user":{"name":"Chandra shankar",
"email":"chandra@gmail.com"
}
}
下面是我在 Session.java 中的 SharedPreferences
public class Session {
private SharedPreferences sp;
private SharedPreferences.Editor spEditor;
public Session(Context context) {
sp = PreferenceManager.getDefaultSharedPreferences(context);
}
public boolean setLogin(boolean status) {
spEditor = sp.edit();
spEditor.putBoolean("is_logged_in", status);
spEditor.commit();
return true;
}
public boolean getLoggedIn() {
return sp.getBoolean("is_logged_in", false);
}
}
【问题讨论】:
-
SharedPreferences 并将其保存为具有私有访问权限的字符串
-
兄弟,我是安卓新手,你能回答我的问题吗?我还需要在我的 MainActivity 中打印存储的数据。
-
离线也能自动登录吗?
-
Bcoz 如果您进行登录,您只需将它们存储在 JSON 变量中,一旦会话时间到期,用户就会超时,清除变量并再次登录.. 变量可以是静态的跨度>
-
是的,现在看到我的登录活动在我进入时具有用户名和密码,它将转到我的 index.php 并通过 JSON 获取状态和用户数据。如果用户 ID 不为空,我想将用户 ID、姓名、电子邮件存储到会话中并在 MainActivity 中显示。我已经在使用会话来检查用户是否登录,如果登录,它将转到 mainactivity,如果不是 loginactivtiy。请查看我的 JSON 回复