【发布时间】:2014-06-11 02:59:57
【问题描述】:
我有一个 android 应用程序,它在成功登录时会在 json 字符串中获取会话 ID。现在我想将该会话 ID 存储在 cookie 中并检索该 cookie 以在进一步请求中再次发送。 现在请告诉我如何持久存储和检索 cookie
【问题讨论】:
标签: android session session-cookies persistent-storage
我有一个 android 应用程序,它在成功登录时会在 json 字符串中获取会话 ID。现在我想将该会话 ID 存储在 cookie 中并检索该 cookie 以在进一步请求中再次发送。 现在请告诉我如何持久存储和检索 cookie
【问题讨论】:
标签: android session session-cookies persistent-storage
您应该看看 SharedPreferences。这将对您有所帮助 http://developer.android.com/guide/topics/data/data-storage.html#pref
【讨论】:
看到这个
public class Session {
private static String PREF_NAME = "Memory";
private static String FBID = "FBID ";
public static boolean saveSessionId(String FBID , Context context) {
Editor editor = context.getSharedPreferences(PREF_NAME, 0).edit();
editor.putString(FBID , FBID );
return editor.commit();
}
public static String getSessionId(Context context) {
SharedPreferences savedSession = context.getSharedPreferences(
PREF_NAME, 0);
return savedSession.getString(FBID , null);
}
}
【讨论】:
会话 id 由服务器为每个会话维护,即会话 id 对于会话保持相同。成功登录标志着会话的开始,成功的注销标志着会话的结束。另一种方法是通过超时结束会话。
例如当您登录时,您会收到一个会话 ID。现在,您可以使用此会话 ID 发出其他请求。当您注销或超时时,此会话 ID 将过期。 timeout 的值取决于应用程序的重要性,即如果它是银行应用程序,那么服务器将在几分钟内使会话超时,而电子商务应用程序可以有更长的超时时间(超出范围:服务器缓存会话)
在您的情况下,请检查您是否正在超时或注销。如果两者都不是,则服务器未维护会话 ID 存在问题。请记住,服务器会为每个会话生成一个唯一的会话 ID。
【讨论】: