7

Hello sir i am new in android and want to persist cookies for all time until user click on signout.but present i am using default Cookiemanger which remove cookies after some hours and my app becomes unresponsive so app not able to make further authentication call to server to get data and app dies.

i have seen many code and use cookie store to keep cookies alive but i failed .i am not able to understand where to write code so that every time i make call to server then cookies automatic goes to server.

Here is my code:

public class PersistentCookieStore implements CookieStore {

/**
 * The default preferences string.
 */
private final static String PREF_DEFAULT_STRING = "";

/**
 * The preferences name.
 */
private final static String PREFS_NAME = PersistentCookieStore.class.getName();

/**
 * The preferences session cookie key.
 */
private final static String PREF_SESSION_COOKIE = "session_cookie";

private CookieStore mStore;
private Context mContext;

/**
 * @param context The application context
 */
public PersistentCookieStore(Context context) {
    // prevent context leaking by getting the application context
    mContext = context.getApplicationContext();

    // get the default in memory store and if there is a cookie stored in shared preferences,
    // we added it to the cookie store
    mStore = new CookieManager().getCookieStore();
    String jsonSessionCookie = getJsonSessionCookieString();
    if (!jsonSessionCookie.equals(PREF_DEFAULT_STRING)) {
        Gson gson = new Gson();
        HttpCookie cookie = gson.fromJson(jsonSessionCookie, HttpCookie.class);
        mStore.add(URI.create(cookie.getDomain()), cookie);
    }
}

@Override
public void add(URI uri, HttpCookie cookie) {
    if (cookie.getName().equals("sessionid")) {
        // if the cookie that the cookie store attempt to add is a session cookie,
        // we remove the older cookie and save the new one in shared preferences
        remove(URI.create(cookie.getDomain()), cookie);
        saveSessionCookie(cookie);
    }

    mStore.add(URI.create(cookie.getDomain()), cookie);
}

@Override
public List<HttpCookie> get(URI uri) {
    return mStore.get(uri);
}

@Override
public List<HttpCookie> getCookies() {
    return mStore.getCookies();
}

@Override
public List<URI> getURIs() {
    return mStore.getURIs();
}

@Override
public boolean remove(URI uri, HttpCookie cookie) {
    return mStore.remove(uri, cookie);
}

@Override
public boolean removeAll() {
    return mStore.removeAll();
}

private String getJsonSessionCookieString() {
    return getPrefs().getString(PREF_SESSION_COOKIE, PREF_DEFAULT_STRING);
}

/**
 * Saves the HttpCookie to SharedPreferences as a json string.
 *
 * @param cookie The cookie to save in SharedPreferences.
 */
private void saveSessionCookie(HttpCookie cookie) {
    Gson gson = new Gson();
    String jsonSessionCookieString = gson.toJson(cookie);
    SharedPreferences.Editor editor = getPrefs().edit();
    editor.putString(PREF_SESSION_COOKIE, jsonSessionCookieString);
    editor.apply();
}

private SharedPreferences getPrefs() {
    return mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}

}

after that i use this code :

 CookieManager cookieManager = new CookieManager(
 new PersistentCookieStore(mContext), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
 CookieHandler.setDefault(cookieManager);

in my launcher activities on create method and it works well for first time .. but after leaving the app in background for hour or two hours it release the cookies and request not send to server because of authentication.

so kindly suggest me right position or activity to add this code or some best way to handle persistent cookies . Ant help would be appreciated in advanced

Anand
  • 429
  • 2
  • 15

0 Answers0