2

I am trying to load a web url on my webview, its a payment URL (page shows the card details entering screen). This URL is working on the browser, its loading on browser bit its not working on android webview. Its log the onPageStarted() and onPageFinished() but its lot showing any errors such as onReceivedError() , onReceivedSslError()

I tried this Some url not loading in webview buts loading in the browser But its also not working with me,

Here is my code,

private void loadPaymentURL(){
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.clearCache(true);
        webView.clearHistory();
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true); // enable javascript
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setAllowFileAccess(false);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setDatabaseEnabled(true);

        webView.setWebViewClient(new MyWebViewClient());
        Log.e("WEBVIEW", "PaymentURL : "+paymentUrl);

        try {
            paymentUrl = getFinalURL(paymentUrl);
        }catch (Exception e){}
        Log.e("WEBVIEW", "FinalURL : "+paymentUrl);

        webView.loadUrl(paymentUrl);
    }

getFinalURL() method I got this from this link Some url not loading in webview buts loading in the browser But its also not working with me,

public static String getFinalURL(String url) throws IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    con.setInstanceFollowRedirects(false);
    con.connect();
    con.getInputStream();

    if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
        String redirectUrl = con.getHeaderField("Location");
        return getFinalURL(redirectUrl);
    }
    return url;
}

Here is my WebViewClient for the webview,

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

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.e("WEBVIEW", "onPageStarted() : "+url);
            progressLayout.setVisibility(View.VISIBLE);
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e("WEBVIEW", "onPageFinished() : "+url);
            progressLayout.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            Log.e("WEBVIEW", "onReceivedError1() : "+error);
            view.goForward();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e("WEBVIEW", "onReceivedError2() URL: "+failingUrl+" , Description:"+description);
            view.goForward();
            progressLayout.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            Log.e("WEBVIEW", "onReceivedSslError() : "+error.toString());
            //progressLayout.setVisibility(View.GONE);
            view.goForward();
        }


    }
2
  • can you add url which is not loading in webview but working fine on browser Commented Jan 30, 2023 at 8:56
  • its something like pay.expresspay.sa/auth/… Commented Jan 30, 2023 at 9:01

1 Answer 1

1

I tried my best to figure out issue but unfortunately not able to get cause of url not loading .

but if it urgent for you , you can go with this github library as it is working fine ..

https://github.com/delight-im/Android-AdvancedWebView

code

private fun loadUrl() {
   val webView = findViewById<AdvancedWebView>(R.id.webview)
   webView.loadUrl("https://pay.expresspay.sa/auth/ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKU1V6STFOaUo5LmV5SnBZWFFpT2pFMk56VXdOamt5TWpFc0ltcDBhU0k2SWpnMk56bGtOell5TFdFd04yTXRNVEZsWkMxaVlXUXlMVE5sTmpFMFltTXpOMkZrTVNJc0ltVjRjQ0k2TVRZM05UQTNNamd5TVgwLlBNNm0xMDRYcVRpNGluSGFhdUQ3eFpkQXZ2TTRsTWU3bEZscld2MUtwZkkwUG0tQUZrd29ubURCaHNhMlJBY3Q0QVd1QWYycDR0N1JlNmJGVWxJLUpsV2ktcXNYUHRjOUVUTFFqUEhaLVNfYjdValI4YzFXYlZNSXZjbGpINVVVN2ZXdkN4S2RYV0w2UWhpZXNKWVJMQmFfeHpORFllWEQwekR1R3JEaFJZeXdIRXFMem8ya3NQZ01HX0U3TEY4UTlrNk9JTTVvYk5HZ095cWw1cm93RjdrbTdyMzg5M0xjV180TXFNV3RwVDE5MTV1dW")
}

enter image description here

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

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.