Skip to content Skip to sidebar Skip to footer

How To Store The Token In Local Or Session Storage In Android?

I'm creating an app that interacts with SOAP web-services to get data from the database. When the user successfully logins it generates a token via web-services. This token will b

Solution 1:

public class CommonUtilities {

    private static SharedPreferences.Editor editor;
    private static SharedPreferences sharedPreferences;
    private static Context mContext;

/**
     * Create SharedPreference and SharedPreferecne Editor for Context
     *
     * @param context
     */
    private static void createSharedPreferenceEditor(Context context) {
        try {
            if (context != null) {
                mContext = context;
            } else {
                mContext = ApplicationStore.getContext();
            }
            sharedPreferences = context.getSharedPreferences(IConstants.SAMPLE_PREF, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

/**
 * Put String in SharedPreference Editor
 *
 * @param context
 * @param key
 * @param value
 */
public static void putPrefString(Context context, String key, String value) {
    try {
        createSharedPreferenceEditor(context);
        editor.putString(key, value);
        editor.commit();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

}

Use this putString() method to store a token when you logged in. And remove that token when you logged out or token expires.


Post a Comment for "How To Store The Token In Local Or Session Storage In Android?"