0

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

{"tag":"login",
 "error":false,
 "user_id":"5",
     "user":{"name":"Chandra shankar",
             "email":"chandra@gmail.com"
            }
}

below is my SharedPreferences in 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);
    }

}
Asesha George
  • 2,232
  • 2
  • 32
  • 68
  • SharedPreferences and save it as String with private access – Zuko Oct 20 '16 at 14:22
  • bro i am new to android can you answer my question. also i need to print the stored data in my MainActivity. – Asesha George Oct 20 '16 at 14:24
  • Do you want to have auto login even when offline? – Zuko Oct 20 '16 at 14:50
  • Bcoz if your doing login, you'd just store them in a JSON variable and just timeout the user once session time expired, clear the variable and they login again.. the variable can be static – Zuko Oct 20 '16 at 14:52
  • yes see now my Login Activity having username and password when i enter, it will go to my index.php and get status and user data through JSON. if userid is not null i want to store user id , name , email in to session and display it in MainActivity. i am already using session to check user logged in or not if logged in it will go to mainactivity if not loginactivtiy. please see my JSON response – Asesha George Oct 20 '16 at 15:11

2 Answers2

1

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");

EDIT

public class Session {
    private SharedPreferences sp;
    private SharedPreferences.Editor spEditor;
    private String ISLOGIN = "is_logged_in";
    private String USER_ID = "user_id";

    public Session(Context context) {
        sp = PreferenceManager.getDefaultSharedPreferences(context);

    }


    public boolean setLogin(boolean status) {
        spEditor = sp.edit();
        spEditor.putBoolean(ISLOGIN, status);
        //use apply for async process
        spEditor.apply();
        return status;
    }

    public boolean getLoggedIn() {
        return sp.getBoolean(ISLOGIN, false);
    }

    public void setUserId(String userid){
        sp.edit().putString(USER_ID,userid).apply();
    }


    //get user id
    public String getUserId(){
       return sp.getString(USER_ID);
    }

}

// in activity

Session session = new Session(this);
String id = session.getUserId();

p/s: i recommended you to use this TinyDB SharedPreferences

ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • ok thank you for response. please see my code i just add my SharedPreferences which i am using. where i need to add the userid as session – Asesha George Oct 20 '16 at 14:21
0

So, you need two steps actually. You need to parse the response and then you need to save it .

For parsing json response , you can build a POJO class. GSON will be helpful for that. You can follow this link to parse response as well .

Then you can save the data to Shared Preference/ Sqlite / Local Variables / Files or any other places and use the saved value based on your need.

Community
  • 1
  • 1
Aveek
  • 849
  • 1
  • 12
  • 28