I have a Fragment with a WebView
and I want set a cookie that I have get her string "name=value".
The following SO links recommend to do a sleep after doing "cookieManager.removeSessionCookie()".
Android WebView Cookie Problem
The code of my AsyncTask is that follows:
private class WebViewTask extends AsyncTask<Void, Void, Boolean> {
String sessionCookie;
CookieManager cookieManager;
@Override
protected void onPreExecute() {
CookieSyncManager.createInstance(getActivity());
cookieManager = CookieManager.getInstance();
sessionCookie = cookieManager.getCookie(BASE_URL);
if (sessionCookie != null) {
sessionCookie = sessionCookie + "; domain=" + BASE_URL;
/* delete old cookies */
cookieManager.removeSessionCookie();
}
super.onPreExecute();
}
protected Boolean doInBackground(Void... param) {
/* this is very important - THIS IS THE HACK */
SystemClock.sleep(1000);
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (sessionCookie != null) {
cookieManager.setCookie(BASE_URL, sessionCookie);
CookieSyncManager.getInstance().sync();
}
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(false);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
mWebView.loadUrl(EDIT_URL);
}
}
In that code I get correctly the sessionCookie with the format: "name=value" and later I add ;domain=BASE_URL.
How I set the session cookie correctly to the WebView?
PD:
mWebView is the
WebView
inside a fragment that I want to set the cookie.This
AsyncTask
is executed from theonCreateView
method of aFragment
after getting the mWebView object.If it helps, I use robospice and retrofit libraries.
Others SO questions checked:
Thanks in advance.