2

I am integrating payment gateway in my application using WebView. I want to read the response from HTML content basically it is a JSON object. This is working fine on 6.0 devices. But for 7.0 and 8.0 I am facing issue with JavaScript. I am using the below code.

My Code

    vPayment.loadUrl(myurl);

    wvPayment.getSettings().setJavaScriptEnabled(true);
    wvPayment.getSettings().setDomStorageEnabled(true);
    wvPayment.clearCache(true);
    wvPayment.clearHistory();
    wvPayment.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wvPayment.addJavascriptInterface(iface, "HTMLOUT");
    wvPayment.setWebChromeClient(new WebChromeClient());
    wvPayment.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (request.getUrl().toString().contains("mysuuccessurl")) {
                try {
                    String html = URLDecoder.decode(request.getUrl().toString(), "UTF-8").substring(9);
                    System.out.println("html" + html);
                } catch (UnsupportedEncodingException e) {
                    Log.e("example", "failed to decode source", e);
                }
            }

            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            System.out.println("Start url : " + url);
            customProgressDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            System.out.println("Finish url : " + url);
            if (url.contains(mysuccessurl)) {
                wvPayment.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
            }
            customProgressDialog.dismiss();
        }
    });
JIFace iface = new JIFace();


class JIFace {
    @android.webkit.JavascriptInterface
    public void showHTML(String data) {
       //want to handle the response here

        }
    }
}

What is wrong with the above code. What changes needs for this work on 7.0 and above android devices

2
  • change window.HTMLOUT to HTMLOUT Commented Mar 6, 2018 at 5:36
  • Same issue JavaScript is currently disabled or is not supported by your browser Commented Mar 6, 2018 at 6:05

2 Answers 2

2

From Android doc

This is pre Android N

@Deprecated
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return false;
}

This is since Android N

@return True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

@Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return true;
    }

This method is from Android N , so for this reason you have this issue only in Android N. Returning false you should solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

0

For anyone also stumbling upon this I've found that android versions below 7 allow ".cshtml" files to be rendered but from android 7 nougart onwards (7.0,7.1,8.0,8.1 etc...) will only output their source code.

I was using some cshtml files generated by a asp.net project and I'd copied the files out of that project and into my xamarin project.

Changing the extension of the files to .html fixed it for me. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.