1

I searched a lot now for the persistence of Cookies but cant find a good solution.

I use HTTPUrlConnection to authenticate against a server and I get Cookies back. I retrieve them in CookieManager and can load them into a new connection. Now I want to persistent that Cookies, perhaps the whole Cookiemanager Object. I found a solution, that you can persist the Cookies with specifying a CookieStore by creating the CookieManager.

I found only old solutions (2-3 years) that say you have to build your own persistent CookieStore, since a persistent CookieStore is not implemented in the SDK? Is this up to date? Is there already a persistent CookieStore implemented in the SDK or does I have to persist the Cookies by myself with SharedPreferences? Or does anybody has a better solution to persist Cookies nowadays?

Best Regards,

4

1 回答 1

0

这似乎在其他一些地方得到了回答,我没有尝试过,但您可以在此链接中获得详细说明:

http://blog.winfieldpeterson.com/2013/01/17/cookies-in-hybrid-android-apps/

public class YourApplication extends Application {
  public void onCreate() {
    super.onCreate();

    //Setup Cookie Manager and Persistence to disk
    CookieSyncManager.createInstance(this);
    CookieManager.getInstance().setAcceptCookie(true);
  }
}

public class MainActivity extends BaseActivity {
  public void onResume() {
    CookieSyncManager.getInstance().stopSync();
  }

   public void onPause() {
     CookieSyncManager.getInstance().sync();
   }
}

API21 弃用了 CookieSyncManager,现在确保将 cookie 写入磁盘使用:

CookieManager.flush()
于 2015-09-13T16:02:39.073 回答