how to save cookies in android app?

你说的曾经没有我的故事 提交于 2020-01-07 08:55:25

问题


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.


回答1:


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.




回答2:


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.
        }
    }
}


来源:https://stackoverflow.com/questions/46356961/how-to-save-cookies-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!