我的LoginActivity正在向URL發送用戶名和密碼並獲得成功狀態。我想在會話中存儲json響應,如userid,名稱,電子郵件信息,並在MainActivity中檢索它。我正在使用下面的代碼json響應。將json響應數組存儲到會話中
JSONObject jObj = new JSONObject(response);
String userId = jObj.getString("user_id");
我的回答是
{"tag":"login",
"error":false,
"user_id":"5",
"user":{"name":"Chandra shankar",
"email":"[email protected]"
}
}
下面是我在Session.java
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並將其保存爲字符串與私人訪問 – Olu
兄弟我是新來的android可以回答我的問題。我也需要在MainActivity中打印存儲的數據。 –
即使離線時也想自動登錄嗎? – Olu