Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Magento 2 e-commerce Website not Working in android Webview

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){


        }
like image 618
Sadia Qamar Avatar asked Nov 09 '22 05:11

Sadia Qamar


1 Answers

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.

like image 56
Shreeya Chhatrala Avatar answered Dec 02 '22 20:12

Shreeya Chhatrala