在我的 Android 应用程序中,每个 Activity 都填充了来自网络某处的 xml 文件的数据。提供这些文件的网站有一个登录机制,可以使用 cookie。
我知道如何向登录页面发出 HTTP 请求并接收 cookie。我不知道的是,我如何存储它以便在其他 Activity 以及下次启动应用程序时重新使用它。 cookie 的有效期为一年,因此我的应用程序的用户应该登录一次,然后一整年都不要再登录。
我该怎么做?我google了很多,但是要么我使用了错误的关键字,要么互联网上没有简单的解决方案。我希望这里有人可以帮助我。
提前致以最诚挚的问候和感谢,扬·奥利弗
最佳答案
使用 CookieSyncManager存储您的 cookie 值。它可以在应用程序启动时持续存在。
注意在 WebViewClient#shouldInterceptRequest 中使用 CookieSyncManager在奇巧。 It will deadlock .
编辑
CookieSyncManager
在 API 21 中已弃用:
This class was deprecated in API level 21. The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method flush() which is a synchronous replacement for sync().
关于Android:如何存储 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4082799/