如何在Android应用中保存cookie?[英] how to save cookies in android app?

本文是小编为大家收集整理的关于如何在Android应用中保存cookie?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

API创建并将数据保存在cookie中.它在浏览器上效果很好.但是该cookie并未保存在Android应用中.始终显示一个空白的数组.有人知道吗? Android开发人员是否必须添加任何库来保存cookie?或者是API侧问题.

推荐答案

您可以尝试一下.

/**
 * Checks the response headers for session cookie and saves it if it
 * finds it.
 * 
 * @param headers
 * Response Headers.
 */
public void checkSessionCookie(Map<String, String> headers)
{
    // Config.SET_COOKIE_KEY = "Set-Cookie"
    if (headers.containsKey(Config.SET_COOKIE_KEY) && headers.get(Config.SET_COOKIE_KEY).startsWith(Config.SESSION_COOKIE))
    {
        String cookie = headers.get(Config.SET_COOKIE_KEY);
        if (cookie.length() > 0)
        {
            String[] splitCookie = cookie.split(";");
            String[] splitSessionId = splitCookie[0].split("=");
            cookie = splitSessionId[1];
            // Now here set cookie to your Preference file.
        }
    }
}

其他推荐答案

参考

#how #how以在Android App中将数据存储在Android App!

在此选项中,您可以使用#Shared Preferences并存储用户信息并通过应用程序访问.

本文地址:https://www.itbaoku.cn/post/1937801.html

问题描述

API creating and save data in a cookie. It works well on the browser. But that cookie is not saved in the android app. Always show a blank array. Anybody know about this? Do android developer has to add any library to save cookie? or it is API side problem.

推荐答案

You can try this.

/**
 * Checks the response headers for session cookie and saves it if it
 * finds it.
 * 
 * @param headers
 * Response Headers.
 */
public void checkSessionCookie(Map<String, String> headers)
{
    // Config.SET_COOKIE_KEY = "Set-Cookie"
    if (headers.containsKey(Config.SET_COOKIE_KEY) && headers.get(Config.SET_COOKIE_KEY).startsWith(Config.SESSION_COOKIE))
    {
        String cookie = headers.get(Config.SET_COOKIE_KEY);
        if (cookie.length() > 0)
        {
            String[] splitCookie = cookie.split(";");
            String[] splitSessionId = splitCookie[0].split("=");
            cookie = splitSessionId[1];
            // Now here set cookie to your Preference file.
        }
    }
}

其他推荐答案

kindly refer,

#How to store data locally in an Android app!

among this options, you can use #Shared Preferences and store user information and access it through out the application.