请求不使用PersistentCookieStore保存的cookies [英] Request doesn't used saved cookies in PersistentCookieStore
问题描述
我定格在我的应用程序崩溃和错误,当我宣布一个cookie商店,但它不保存cookies或者出事了以其他职位。
I fixed the crash and error in my app when I declared a cookie store, but it doesn't save the cookies or something went wrong at an other position.
起初,我把这些2号线:
At first I call these 2 lines:
AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookieStore;
然后,我有一个POST:
And then I have a POST:
public void postRequestLogin(String url, RequestParams params) {
myCookieStore = new PersistentCookieStore(this);
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
client.setCookieStore(myCookieStore);
System.out.println(response);
if(response.contains("Login successful!")) {
TextView lblStatus = (TextView)findViewById(R.id.lblStatus);
lblStatus.setText("Login successful!");
getRequest("url");
} else {
TextView lblStatus = (TextView)findViewById(R.id.lblStatus);
lblStatus.setText("Login failed!");
TextView source = (TextView)findViewById(R.id.response_request);
source.setText(response);
}
}
});
}
那么就应该保存Logincookies,并用它为GET请求:
Then it should save the Logincookies and use it for the GET Request:
public void getRequest(String url) {
myCookieStore = new PersistentCookieStore(this);
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
client.setCookieStore(myCookieStore);
System.out.println(response);
TextView responseview = (TextView) findViewById(R.id.response_request);
responseview.setText(response);
}
});
}
但是它不使用的cookie。当我做的GET请求我已经退出。
But it doesn't use the cookies. When I do the GET Request I'm already logged out.
编辑:我忘了说,我用一个lib从本教程:的http://循环J。 COM / Android的异步HTTP /
I forgot to say that I use a lib from this tutorial: http://loopj.com/android-async-http/
推荐答案
我认为这个问题是您设置的cookie存储后请求已经完成(在的onSuccess
法)。尝试设置它,你作出这样的请求之前:
I think the problem is that you set the cookie store after the request has already completed (in the onSuccess
method). Try setting it before you make that request:
myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
client.post(url, params, new AsyncHttpResponseHandler() {
你还创建在每次请求一个新的cookie存储。如果你有多个请求,会发生什么?这将创建一个新的Cookie存储和使用它(和新的cookie存储不会有你的cookies)。尝试code,这部分转移到你的构造函数:
You're also creating a new cookie store on every request. What happens if you do more than one request? It will create a new cookie store and use it (and the new cookie store won't have your cookies). Try moving this part of the code to your constructor:
myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
再从其他功能将其删除。
Then remove it from the other functions.
这篇关于请求不使用PersistentCookieStore保存的cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!