- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在声明 cookie 存储时修复了我的应用程序中的崩溃和错误,但它没有保存 cookie 或在其他位置出现问题。
起初我调用这两行:
AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookieStore;
然后我有一个 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 请求:
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 请求时,我已经注销了。
编辑:我忘了说我使用了本教程中的库:http://loopj.com/android-async-http/
最佳答案
我认为问题在于您在请求完成后设置了 cookie 存储(在 onSuccess
方法中)。在发出该请求之前尝试设置它:
myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
client.post(url, params, new AsyncHttpResponseHandler() {
您还针对每个请求创建了一个新的 cookie 存储。如果您执行多个请求会怎样?它将创建一个新的 cookie 存储并使用它(新的 cookie 存储不会有您的 cookie)。尝试将这部分代码移到您的构造函数中:
myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
然后将其从其他函数中移除。
关于java - 请求不使用 PersistentCookieStore 中保存的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668186/
我有一个 PersistentCookieStore,我想在全局范围内使用它,这意味着它会在 login.java 中将 cookie 写入其中,并且我想在 mainactivity.java 中使用
我在声明 cookie 存储时修复了我的应用程序中的崩溃和错误,但它没有保存 cookie 或在其他位置出现问题。 起初我调用这两行: AsyncHttpClient client = new Asy
我正在开发一个 Android 应用程序,其中向网络服务发出登录后请求。该请求返回一个 20 分钟后过期的 cookie。 使用 okhttp3 和这个 PersistentCookieStore库,
我是一名优秀的程序员,十分优秀!