如何解决Android WebView中的黑屏?

时间:2019-07-08 11:46:04

标签: java android android-webview

我正在https://turnbackhoax.id/内设置网址WebView。但是WebView仅显示空白页。我试图设置其他URL,这些工作。我已经学习并搜索了类似的问题,但没有任何改变。

以下是我的WebView代码:

public class LaporPage extends AppCompatActivity {

    private WebView webV;
    Activity activity;
    private ProgressDialog progDailog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lapor_page);

        activity = this;

        progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
        progDailog.setCancelable(false);

        webV = (WebView) findViewById(R.id.web_view);

        webV.getSettings().setDomStorageEnabled(true);
        webV.getSettings().setJavaScriptEnabled(true);
        webV.getSettings().setLoadWithOverviewMode(true);
        webV.getSettings().setUseWideViewPort(true);
        webV.setWebChromeClient(new WebChromeClient());
        webV.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progDailog.show();
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageFinished(WebView view, final String url) {
                progDailog.dismiss();
            }
        });
        webV.loadUrl("https://turnbackhoax.id/");
    }
}

6 个答案:

答案 0 :(得分:2)

我检查了URL,它在Web视图中引发SSL握手错误。尽管忽略SSL错误不是一个好习惯,但这可以解决您的问题

webview.setWebViewClient(new WebViewClient(){

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,SslError error) {
        handler.proceed();
    }


});

答案 1 :(得分:0)

在您的Manifest.xml中添加此权限

  <uses-permission android:name="android.permission.INTERNET" />

使用此:

 wv = (WebView) findViewById(R.id.web_view);
        wv.setInitialScale(1);      //webview page matches the screen size.
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);    //loads online website when no internet connection.
        wv.loadUrl("https://prajwalwaingankar.wixsite.com/nivala");

答案 2 :(得分:0)

您需要使用重写onReceivedSslError

尝试

webV.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progDailog.show();
            view.loadUrl(url);

            return true;
        }
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
            handler.proceed();
        }
        @Override
        public void onPageFinished(WebView view, final String url) {
            progDailog.dismiss();
        }

    });

答案 3 :(得分:0)

创建名为network_security_config.xml的文件,

<network-security-config>
<base-config cleartextTrafficPermitted="true">
    <debug-overrides>
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
    </debug-overrides>
</base-config>
<domain-config>
    <domain includeSubdomains="true">example.com</domain>
    <domain includeSubdomains="true">example.example.com</domain>
    <trust-anchors>
        <certificates src="@raw/mycert" />
        <certificates src="system" />
    </trust-anchors>
</domain-config>

在清单中,

<application ..... 
android:networkSecurityConfig="@xml/network_security_config"

</application>

并根据需要添加您的SSL

答案 4 :(得分:0)

要根据新的安全策略正确处理SSL证书验证并避免Google拒绝应用程序,请更改代码以在服务器提供的证书满足您的期望时调用 SslErrorHandler.proceed()。调用SslErrorHandler.cancel()

webV.setWebViewClient(new  WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                webV!!.loadUrl(url)
                return true
            }

            override fun onReceivedSslError(
                view: WebView, handler: SslErrorHandler,
                error: SslError
            ) {
                handler.proceed()
            }
        });

答案 5 :(得分:0)

其简单而准确的解决方案是启用 DomStorage 并设置 setJavaScriptCanOpenWindowsAutomatically = true

    WebView webView = findViewById(R.id.webView);        

    webView.getSettings().setDomStorageEnabled(true);    // this 
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);  //and this

它会解决问题。

享受吧。