Search code examples
androidjsonsharedpreferencesstoreserver

Saving user_id responce from JSON to sharepreferance


can any one help me out I really need a solution, I have already search on SO, could not success !

I want to store auto generated user_id which I am receiving from server as a JSON response in my share preference and then again get from share preference to other activity and send as a parameter to server

Here is my login Activity where I am receiving unique user id in JSON response when sending username and pass to server

    public void connect(String useremail, String userpassword) throws JSONException, IOException
{
RestClient1 client = new RestClient1(Constants.serverPath + Constants.loginMethod);

client.addParam("email", useremail);
client.addParam("password", userpassword);
client.addHeader("content-type", "application/json");
try
{
String response = client.executePost();
JSONObject jsonResponse = new JSONObject(response);
String jsonData = jsonResponse.getString("code");
String jData = jsonResponse.getString("ResponseStatus");
jsData = jData;
if (jsonData.equals("200"))
{
System.out.println("responseStatus =" + jData);
startActivity(new Intent(LoginMainActivity.this, DashBoardActivity.class));
finish();
}



In the "ResponseStatus" I am geting "Login Succsesfull , user_id=1" from server <**<< want to store this response on Share Preference** 

My Basic Session Manager class which is not fully construct for storing response in JSON

    public class SessionManager {
        // Shared Preferences
        SharedPreferences pref;

        // Editor for Shared preferences
        Editor editor;

        // Context
        Context _context;

        // Shared pref mode
        int PRIVATE_MODE = 0;

        // Sharedpref file name
        private static final String PREF_NAME = "Apppersonal";

        // All Shared Preferences Keys

         private static final String KEY_USERID = "user_id";    
      //  public static final String KEY_FULLNAME = "fullName";    
      //  public static final String KEY_EMAIL = "email";   
      //  public static final String KEY_DOB = "dateofbirth";    
       // public static final String KEY_ADDRESS = "address";


        // Constructor
        public SessionManager(Context context){
            this._context = context;
            pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
            editor = pref.edit();
        }
     public void createloginSession(int user_id)
       {
           // Storing id in pref
            editor.putInt(KEY_USERID, user_id);        

            editor.commit();

        }
      public HashMap<String, String> getPersonalDetails() {

    HashMap<String, String> userPersonal = new HashMap<String, String>();
    userPersonal.put(KEY_FULLNAME, pref.getString(KEY_FULLNAME, null));
    userPersonal.put(KEY_DOB, pref.getString(KEY_DOB, null));  
    userPersonal.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
    userPersonal.put(KEY_ADDRESS, pref.getString(KEY_ADDRESS, null));
    // return user
   return userPersonal;
    // TODO Auto-generated method stub
//  return null;
}



// Clear session details
public void logoutUser(){
    // Clearing all data from Shared Preferences
    editor.clear();
    editor.commit();

    // After logout redirect user to Loing Activity
    Intent i = new Intent(_context, LoginMainActivity.class);
    // Closing all the Activities
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Add new Flag to start new Activity
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Staring Login Activity
    _context.startActivity(i);
}

}

Solution

  • After getting the user_id just do the following:

    SessionManager mSessionManager = new SessionManager(context);
    mSessionManager.createloginSession(user_id);