Firdoesh
Firdoesh

Reputation: 93

Shared Prefrences to save a authentication token in android

I have never used sharedprefrences before, now what i want to store a authentication token in sharedprefrences. This is my code where i am getting user token in hawkerauthToken which is a string, i want to store this token in sharedprefrences

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            VolleyLog.v("v_response", response.toString(4));
                            Log.d("resopnse", response.toString());

                            hawkerauthToken = String.valueOf(response.get("token"));
                            Log.d("tokeennnn" , hawkerauthToken);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

Upvotes: 0

Views: 6725

Answers (2)

Sohail Zahid
Sohail Zahid

Reputation: 8149

SharedPreferences

Save:

 SharedPreferences preferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
 preferences.edit().putString("token", hawkerauthToken).commit();

retrieve:

   String token = preferences.getString("token","");

Upvotes: 6

subrahmanyam boyapati
subrahmanyam boyapati

Reputation: 2878

Try below code

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(inboxFragment.context);
        sp.edit().putString("keyname", "keyValue").apply();

Upvotes: 0

Related Questions