Asesha George Asesha George - 1 year ago 77
JSON Question

Store json response array in to session

My LoginActivity is sending username and password to URL and getting success status. I want to store json response like userid, name, email information in a session and retrieve it in MainActivity. I am using below code for json response.

JSONObject jObj = new JSONObject(response);
String userId = jObj.getString("user_id");

and my response is

"user":{"name":"Chandra shankar",

below is my SharedPreferences in

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);
return true;

public boolean getLoggedIn() {
return sp.getBoolean("is_logged_in", false);


Answer Source

use SharedPreferences.

SharedPreferences preferences = this.getSharedPreferences("APP_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

editor.putString("ID", userId).apply();

if you want to get the value back.

String id = preferences.getString("ID");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download