使用 HttpClient 时跨活动使用 Cookie
我正在尝试一个简单的应用程序来阅读网站的HTML,并将其转换以在Android中创建一个易于阅读的UI(这是学习Android的exersize,而不是制作一个可用的应用程序)。我遇到的问题是跨活动持久保存用户会话,然后在一旦召回时使用该会话。HttpClient
我想“正确”地执行此操作,推荐的方法似乎是使用.然而,我遇到了问题 - 我似乎找不到“正确”的方式从 中获取a并在以后的单独活动中实例化使用它。CookieManager
Cookie
CookieManager
HttpClient
使用时,我可以保存 和 然后 在其他活动的作用域中(请参阅代码片段 2)。我以后在请求页面时没有找到如何使用它(请参阅代码片段3)。CookieManager
Cookie
Cookie
说得够多了,这里有一些代码。首先,我的登录操作和 Cookie 存储:
private OnClickListener loginActionListener = new OnClickListener()
{
public void onClick(View v)
{
EditText usernameTextView = (EditText) findViewById(R.id.Username);
EditText passwordTextView = (EditText) findViewById(R.id.Password);
String username = usernameTextView.getText().toString();
String password = passwordTextView.getText().toString();
try {
HttpPost postMethod = new HttpPost(URI);
HttpParams params = new BasicHttpParams();
params.setParameter("mode", "login");
params.setParameter("autologin", true);
params.setParameter("username", username);
params.setParameter("password", password);
postMethod.setParams(params);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(postMethod);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if(cookies != null)
{
for(Cookie cookie : cookies)
{
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);
}
}
CookieSyncManager.getInstance().sync();
Intent intent = new Intent(v.getContext(), IndexAction.class);
startActivity(intent);
} catch (Exception e) {...}
}
决定是让用户登录还是转到索引的启动活动如下。从此代码中可以看出,Cookie 在范围内,可以读取:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(this);
if(CookieManager.getInstance().getCookie(URI) == null)
{
Intent intent = new Intent(this, LoginAction.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(this, IndexAction.class);
startActivity(intent);
}
}
但是从我的代码中阅读索引页面,我希望你能建议我错过了什么:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(this);
try
{
HttpGet getMethod = new HttpGet(URI_INDEX);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 30000);
HttpConnectionParams.setSoTimeout(params, 30000);
// This code results in a ClassCastException, I'm assuming i've found a red herring with this solution.
// HttpContext localContext = new BasicHttpContext();
// localContext.setAttribute(ClientContext.COOKIE_STORE, CookieManager.getInstance().getCookie(URI));
DefaultHttpClient httpClient = new DefaultHttpClient(params);
HttpResponse response = httpClient.execute(getMethod);
if(response.getStatusLine().getStatusCode() > 299 && response.getStatusLine().getStatusCode() < 400)
{
// Not logged in doesn't give a redirect response. Very annoying.
}
final char[] buffer = new char[0x10000];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(response.getEntity().getContent(), "UTF-8");
int read = 0;
while (read>=0)
{
read = in.read(buffer, 0, buffer.length);
if (read>0) {
out.append(buffer, 0, read);
}
}
String returnString = out.toString();
} catch (ClientProtocolException e) {...}
}
on 没有使用 Cookie(在调试中仔细检查了这一点)来拉回页面。如果有人能填补我知识中的这个漏洞,那就太好了。HttpClient
execute(getMethod)
提前致谢。
编辑
当注释的代码被添加回去(将方法更改为 )时,将生成此跟踪跟踪 - 假设是因为我正在用 a 而不是 :httpClient.execute(getMethod)
httpClient.execute(getMethod, localContext)
ClientContext.COOKIE_STORE
Cookie
String
CookieStore
*org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:88), org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290), org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160), org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555), org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487),
com.testapp.site.name.IndexAction.onCreate(IndexAction.java:47),
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047),
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611),
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663),
android.app.ActivityThread.access$1500(ActivityThread.java:117),
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931),
android.os.Handler.dispatchMessage(Handler.java:99),
android.os.Looper.loop(Looper.java:123),
android.app.ActivityThread.main(ActivityThread.java:3683),
java.lang.reflect.Method.invokeNative(Native Method),
java.lang.reflect.Method.invoke(Method.java:507),
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839),
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597),
dalvik.system.NativeStart.main(Native Method)*