0

I am trying to load cookie in android web view. following is my java code.

final String link = "http://www.iitjeeacademy.com/mobile/question/CHE/AAH";

final String domain = "iitjeeacademy.com";

String authToken = LoginService.getCookie();

String cookie = "auth-token=" + authToken + "; csrf-token=mobile";

webView = (WebView) findViewById(R.id.webView);

Map<String, String> cookieMap = new HashMap<String, String>();

cookieMap.put("Cookie", cookie);


android.webkit.CookieSyncManager.createInstance(QuestionActivity.this);
android.webkit.CookieManager.getInstance().setAcceptCookie(true);

WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);

java.net.CookieHandler.setDefault(coreCookieManager);

android.webkit.CookieSyncManager.getInstance().sync();

final WebSettings settings = webView.getSettings();
settings.setLoadsImagesAutomatically(true);
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);

webView.setWebViewClient(new CustomBrowser());
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(link, cookieMap);

private class CustomBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        view.loadUrl(url);
        return true;
    }
}

WebView layout.xml

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

I tried to implement WebkitCookieManagerProxy for custom cookie storage as shown in -> Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager).

But it is not working. When I run this code, in web view I am getting redirected to index page of site...

Community
  • 1
  • 1
Omkar
  • 1,493
  • 3
  • 17
  • 36

2 Answers2

2
 WebView webview = (WebView) this.findViewById(R.id.webView);
    final WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setPluginState(WebSettings.PluginState.ON);

    webview.setWebChromeClient(new WebChromeClient());

    CookieSyncManager.createInstance(YourClass.this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    cookieManager.setCookie("your domain name", cookie);
    CookieSyncManager.getInstance().sync();

    Map<String, String> header = new HashMap<String, String>();
    header.put("Cookie", cookie);
    if(isConnected()) {
        webview.loadUrl(url, header);
    }
    else {
        webview.setVisibility(View.GONE);
        textView.setText("Your custom error message.");
        textView.setVisibility(View.VISIBLE);
    }
Aman Grover
  • 1,621
  • 1
  • 21
  • 41
  • why `textView.setText("Your custom error message.");`? where it came from? – Omkar Sep 26 '15 at 11:03
  • Actually in my code I first check if the user is connected to internet or not,if not webview show the url to the user which might be risky in some cases, it is totally upto you how you wanna use this code, the main webview cookie code ends with the loadUrl() method – Aman Grover Sep 26 '15 at 11:05
  • Nope. this is not working... still redirecting to home page :( – Omkar Sep 26 '15 at 11:21
  • If after the login from the user you get some cookie , you need to save those in Shared Preferences and use the same cookies here in the webview – Aman Grover Sep 26 '15 at 11:22
  • Yes, I am using same cookies. But still it is not working. – Omkar Sep 26 '15 at 12:04
0

**This may help **

WebView webview = (WebView) this.findViewById(R.id.wv_file);
    final WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setPluginState(WebSettings.PluginState.ON);

    webview.setWebChromeClient(new WebChromeClient());

    CookieSyncManager.createInstance(ActivityName.this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    String cookieString = "param=value";
    cookieManager.setCookie(domain_of_url("domain name"), cookieString);
    CookieSyncManager.getInstance().sync();

    Map<String, String> abc = new HashMap<String, String>();
    abc.put("Cookie", cookieString);
    webview.loadUrl("domain name",
            abc);

may you get the solution

Also go throught this link to get proper idea Android WebView Cookie Problem

or you can also do as in these qestion Using session cookies with android

and

Using cookies with Android

Community
  • 1
  • 1
Ravikant Paudel
  • 2,228
  • 2
  • 17
  • 28