WebView with Javascript keep reloading

Issue

I am using a WebView to display a website into my application. When I’m loading my WebView, I first need to fill the authentification form and then redirect to the original URL.

My WebView keep reloading again and again. Could anyone explain to me how to use a WebView with “automatic” filling please?

        final WebView mWebView;

        //Show webview into the app via a popup
        AlertDialog.Builder mAlertDialog  new AlertDialog.Builder(context);
        mAlertDialog.setTitle(R.string.menuBuyCredit);

        mWebView  new WebView(context);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.loadUrl(ConstantsClass.URL_BUY_CREDIT);

        mWebView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                //In order to automatically fill the form
                final String mJavaScript  "javascript:" +
                        "document.getElementById('Login').value  '" + ClientSingleton.getInstance().getmLogin() + "';" +
                        "document.getElementById('Password').value  '" + ClientSingleton.getInstance().getmPassword() + "';" +
                        "document.getElementById('Btn_Envoyer').click()";

                view.loadUrl(mJavaScript);
                view.loadUrl(ConstantsClass.URL_BUY_CREDIT);
                view.pageDown(true);
            }

        });

        mAlertDialog.setView(mWebView);
        mAlertDialog.setNegativeButton(R.string.closeDrawer, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        mAlertDialog.show();
    } ```

Solution

You should use something like

Put this in global

boolean loadedAlready  false;

inside onPageFinished method

if (view.getUrl().equals(ConstantsClass. URL_BUY_CREDIT) && !loadedAlready) { 
    loadedAlready true; 
    view.load(ConstantsClass.URL_BUY_CREDIT);
 } 

Answered By – Rahul Khurana

Leave a Comment