I am following this for sharing cookies from native code to WebView
. I have a native login screen. On successful login I am saving the cookies in CookieSyncManager
. And when the webview loads, I am passing those cookies to it so that the login screen does not appears.
Following is what I have implemented :
public class MyApp extends Application {
public void onCreate() {
super.onCreate();
//Setup Cookie Manager and Persistence to disk
CookieSyncManager.createInstance(this);
CookieManager.getInstance().setAcceptCookie(true);
}
}
HttpRequest for login :
private void executeRequest(HttpUriRequest request, String url) {
DefaultHttpClient client = getDefaultClient(); // new DefaultHttpClient();
syncCookiesFromAppCookieManager(loginUrl, client);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(request);
} catch (IOException e1) {
e1.printStackTrace();
}
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = null;
try {
instream = entity.getContent();
} catch (IOException e1) {
e1.printStackTrace();
}
response = convertStreamToString(instream);
response = StringUtils.remove(response, "\n");
syncCookiesFromAppCookieManager(loginUrl, client);
// client.setCookieStore((org.apache.http.client.CookieStore) new PersistentCookieStore(context));
}
}
And in WebActivity :
CookieSyncManager.getInstance().sync();
webview.loadUrl(url);
But I am again getting the login screen on webView
. i.e. cookies are not getting stored.
K7Ko's answer finally worked for me. But only after I commented the line
cookieManager.removeSessionCookie();