问题
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