Async Task, Webview, Cookies - Android
I have a "native" login screen which sets a static cookie and then this activity gets the cookie from LoginWebView. I'm doing this in an async task. Th开发者_StackOverflowe problem now is that the async task contains 3 very ugly hacks which makes the code very unreliable (the hacks are marked out in the code below).
The question is, do you have any idea on how to get rid of one or more of these ugly hacks?
Hack nr 1 - This is taken from an example that someone posted here on stack overflow, the cookie needs to wait a bit before it actually works and loads in the webview, I have no idea why but without it it just does not work. This is not the biggest problem though.
Hack nr 2 - I don't know why but if I want to be able to load another page I have to first load the page that I got the cookie from. When this is loaded I simply wait a small bit and then load another url. Do anyone have any idea why? If I don't do this the webview recognizes that the cookie is there but won't finish loading any pages at all, it just stucks on loading and never finishes.
Hack nr 3 - It's just to remedy hack nr 2.
private class WebViewTask extends AsyncTask<Void, Void, Boolean> {
String cookieString1;
CookieManager cookieManager;
@Override
protected void onPreExecute() {
Cookie sessionCookie = LoginWebView.cookie1;
CookieSyncManager.createInstance(WebViewActivity.this);
cookieManager = CookieManager.getInstance();
cookieString1 = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; " +
"domain=" + sessionCookie.getDomain();
CookieSyncManager.getInstance();
//cookieManager.removeSessionCookie();
super.onPreExecute();
}
protected Boolean doInBackground(Void... param) {
// HACK NR.1
SystemClock.sleep(600);
return false;
}
@Override
protected void onPostExecute(Boolean result) {
Log.v(TAG, "COOKIE SYNC 1: " + cookieString1);
Log.v(TAG, "COOKIE SYNC 1: " + domain);
cookieManager.setCookie(domain, cookieString1);
CookieSyncManager.getInstance().sync();
WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Loading...");
view.loadUrl(url);
return true;
}
});
//HACK NR 2
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.loadUrl(domain);
Log.v(TAG, "PROGRESS " + webView.getProgress());
button1.setChecked(true);
if(webView.getProgress() == 100) {
//HACK NR. 3
SystemClock.sleep(80);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
clickButtonOne();
}
}
}
Could you perhaps try a connecting thread solution I have proposed in android session management A login activity could utilise that. And as for the WebView, you can pass the cookie with (sCookie is the static cookie string already set by the login activity):
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
String cookieString = sCookie+"; domain="+sDOMAIN;
cookieManager.setCookie(sDOMAIN, cookieString);
CookieSyncManager.getInstance().sync();
精彩评论