[英]Android volley keep user logged in with cookies
Straight to the point, I've created a simple login with Google's volley. 直截了当,我已经用Google的凌空创建了一个简单的登录名。 Once the user is logged, I need to keep track of the cookies in order to let him doing some actions.
登录用户后,我需要跟踪cookie,以便让他执行一些操作。 I set up cookies with this function:
我使用以下功能设置了Cookie:
private boolean setUpCookies(){
boolean ris;
cookieManager = new CookieManager(new PersistentCookieStore(getApplicationContext()), CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
SharedPreferences preferences = getSharedPreferences(PersistentCookieStore.class.getName(), MODE_PRIVATE);
String session_cookie = preferences.getString("session_cookie",null);
if(session_cookie != null && !session_cookie.isEmpty()){
Log.d(TAG,session_cookie);
ris = true;
}else{
ris = false;
}
return ris;
}
Where PersistentCookieStore is this: source 其中PersistentCookieStore: 源
Then in my volley onResponse I have this: 然后在我的凌空凌空我有这个:
@Override
public void onResponse(String response) {
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(response);
if (element.isJsonArray()) {
try{
JsonArray array = element.getAsJsonArray();
JsonObject tmp = array.get(0).getAsJsonObject();
if(setUpCookies()){
Toast.makeText(getApplicationContext(), "Login OK", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(MainActivity.this, PostLogin.class);
startActivity(myIntent);
}else{
Toast.makeText(getApplicationContext(), "Could not set up cookies!", Toast.LENGTH_LONG).show();
}
}catch (Exception e){
VolleyLog.d(e.getMessage());
}
} else if (element.isJsonObject()) {
JsonObject object = element.getAsJsonObject();
Toast.makeText(getApplicationContext(), "Error! " + object.get("error").getAsString(), Toast.LENGTH_LONG).show();
}
showProgress(false);
}
Since I can not edit the server php pages, I wonder if I'm doing this correctly, because if I launch the app in the emulator, then I login and get the OK response, then throught Android Device Monitor I delete that preference I'm still able to do stuffs even if the cookies aren't set! 由于我无法编辑服务器php页面,所以我想知道我是否在正确执行此操作,因为如果我在模拟器中启动该应用程序,则登录并获得OK响应,然后在Android Device Monitor中删除该首选项,即使未设置Cookie,m仍然可以做东西! Hope I could explain myself.
希望我能解释自己。
by : 创建人:
cookieManager = new CookieManager(new PersistentCookieStore(getApplicationContext()), CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
you initialize the cookieStore and this is the only time the prefs are read. 您初始化cookieStore,这是唯一一次读取首选项的时间。 Every call will use the in-memory cookies and thus the session will be kept.
每次呼叫都将使用内存中的cookie,因此将保留会话。 If you delete the prefs and want to apply the changes you can either:
如果删除首选项并要应用更改,则可以:
PersistentCookieStore
PersistentCookieStore
you can also just use public boolean remove(URI uri, HttpCookie cookie)
or public boolean removeAll()
which will remove the in-memory cookies but keep the session one in the prefs as this implementation(PersistentCookieStore) does not sync on remove 您也可以只使用
public boolean remove(URI uri, HttpCookie cookie)
或public boolean removeAll()
来删除内存中的cookie,但将会话保持在首选状态,因为此实现(PersistentCookieStore)在删除时不同步
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.