android 未在webview上加载URL,但在浏览器上正确加载

slmsl1lt  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(167)

我试图在我的webview上加载一个web url,它是一个payment URL(页面显示卡详细信息输入屏幕)。这个url在浏览器上工作,它在浏览器上加载位它在android webview上不工作。它记录onPageStarted()onPageFinished(),但它的很多显示任何错误,如onReceivedError()onReceivedSslError()
我试过这个Some url not loading in webview buts loading in the browser,但它也不适合我,
这是我的代码

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()方法我从这个链接Some url not loading in webview buts loading in the browser得到这个,但它也不适合我,

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;
}

这是我的webviewWebViewClient

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();
        }

    }
cunj1qz1

cunj1qz11#

我尽了最大努力来找出问题,但不幸的是无法得到的网址未加载的原因。
但是如果你很急,你可以用这个Github库,因为它工作得很好。
https://github.com/delight-im/Android-AdvancedWebView
电码

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

相关问题