webview在最新版本的android中无法打开某些网站 [英] webview doesn't open some sites in recent versions of android

查看:110
本文介绍了webview在最新版本的android中无法打开某些网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开网络视图的应用程序,发现无法在最新版本的android中打开某些网站.

I have an app that open a webview and I found that I can't open some sites in recent versions of android.

在出现错误找不到页面之前,但我正在搜索并且发现

Before I got the error Page not found but I was searching and I found this link where it say:

android:usesCleartextTraffic

android:usesCleartextTraffic

指示应用程序是否打算使用明文网络流量,例如明文HTTP.面向API级别27或更低级别的应用程序的默认值为"true".面向API级别28或更高级别的应用默认为假".

Indicates whether the app intends to use cleartext network traffic, such as cleartext HTTP. The default value for apps that target API level 27 or lower is "true". Apps that target API level 28 or higher default to "false".

...

所以我在 AndroidManifest.xml 中添加了 android:usesCleartextTraffic ="true" ,现在Web视图是白色的,什么也没显示

So I add android:usesCleartextTraffic="true" in AndroidManifest.xml and now the webview is white and doesn't show nothing

我的网络视图活动:

public class WebviewUrl extends AppCompatActivity {
private WebView webView;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_webview_url);

    openURL();
}

private void openURL() {
    Intent getUrl = getIntent();
    String url = getUrl.getStringExtra("url");

    webView = findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(url);

    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    WebSettings webSettings = webView.getSettings();
    webSettings.setDomStorageEnabled(true);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadsImagesAutomatically(true);

    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

    webView.setWebViewClient(new WebViewClient() {
        @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        } });

    webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    }
    else {
        super.onBackPressed();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    webView.saveState(outState);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    else
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
}

有人可以帮忙吗?

更新:现在再次出现该页面未找到

UPDATE: Now reappeared that the page was not found

推荐答案

我解决了.在我的 AndroidManifest.xml 中,我将这两行一起"并删除了 android:networkSecurityConfig ="@ xml/network_security_config" :

I solved it. In my AndroidManifest.xml I had this 2 lines 'together' and I removed android:networkSecurityConfig="@xml/network_security_config":

<application
    ...
    android:networkSecurityConfig="@xml/network_security_config" <!--I removed that one -->
    android:usesCleartextTraffic="true"
>

这篇关于webview在最新版本的android中无法打开某些网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆