7

您好,先生,我是 android 新手,希望一直保留 cookie,直到用户单击注销。但目前我正在使用默认的 Cookiemanger,它会在几个小时后删除 cookie,并且我的应用程序变得无响应,因此应用程序无法对服务器进行进一步的身份验证调用获取数据和应用程序死亡。

我看过很多代码并使用 cookie 存储来保持 cookie 存活,但我失败了。我无法理解在哪里编写代码,所以每次我调用服务器时,cookie 都会自动转到服务器。

这是我的代码:

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

}

之后我使用这段代码:

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

在我关于创建方法的启动器活动中,它第一次运行良好.. 但是在将应用程序留在后台一小时或两小时后,它会释放 cookie 并请求不发送到服务器,因为身份验证。

所以请建议我正确的位置或活动来添加此代码或一些处理持久性 cookie 的最佳方法。蚂蚁帮助将在高级

4

0 回答 0