5

When I am trying to load a Magento 2 e-commerce website in android WebView, i'm getting following error message:

"Unable to send the cookie. Maximum number of cookies would be exceeded."

What's the cause of this error.? How can I fix this.? Could anyone help me.?

Android Code

WebView webView=(WebView) findViewById(R.id.disp);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.setWebViewClient(new MyViewClient());
        try {
            webView.loadUrl("https://sweetroomksa.com/");
        }catch (Exception e){


        }
Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Sadia Qamar
  • 63
  • 1
  • 6
  • Did you find any answer to this @sadia Qamar ? – B.shruti Aug 22 '17 at 12:43
  • Try to clear the cookies and reload the page. [This SO answer](https://stackoverflow.com/a/31950789/1449010) has an example of how to do that – Victor B Aug 22 '17 at 21:55
  • @VictorB Clearing the cookie has resolved the error but it is causing one major effect i.e if I am already a logged in user, and then i go back and come to app again, it will clear the session and always prompting me to login again. – B.shruti Aug 23 '17 at 05:42
  • What if you clear the cookies only when the webview is not in use? – Victor B Aug 23 '17 at 07:22
  • 1
    Also try to increase the max number of cookies in Magento as explained in [How to fix «Unable to send the cookie. Maximum number of cookies would be exceeded»](https://mage2.pro/t/topic/84) – Victor B Aug 23 '17 at 07:25

1 Answers1

1

Create your custom WebViewClient class and then try to load WebView url like below:

WebViewClientImpl.java

public class WebViewClientImpl extends WebViewClient {

    private Activity activity = null;

    public WebViewClientImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if(url.indexOf("/sweetroomksa.com/") > -1 )
            return false;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        WebViewClientImpl webViewClient = new WebViewClientImpl(this);
        webView.setWebViewClient(webViewClient);

        webView.loadUrl("https://sweetroomksa.com/");
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
            this.webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

Above code working perfectly in my device.
Hope above answer may help you.

Shreeya Chhatrala
  • 1,441
  • 18
  • 33