【问题标题】:Android WebView failed to load (net::ERR_CLEARTEXT_NOT_PERMITTED)Android WebView 加载失败 (net::ERR_CLEARTEXT_NOT_PERMITTED)
【发布时间】:2019-06-29 18:53:13
【问题描述】:

有人可以帮帮我吗?我在我的 Android 应用中使用 WebView

compileSdkVersion 29
buildToolsVersion "29.0.0"
minSdkVersion 16
targetSdkVersion 29

我在 AmdroidManifest 中有配置 https 并创建了一个配置文件,但没有更改我收到明文错误:

(净::ERR_CLEARTEXT_NOT_PERMITTED)

@xml/network_security_config

  <?xml version="1.0" encoding="utf-8"?>
  <network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">www.mydomaine.com</domain>
  </domain-config>
  </network-security-config>

AndroidManifest.xml

    <manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:hardwareAccelerated="true"
    android:networkSecurityConfig="@xml/network_security_config"
    android:usesCleartextTraffic="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

WabView 类

    public void LoadWeb() {
    webView = (WebView) findViewById(R.id.webview_test);
    webView.getSettings().setAppCacheEnabled(false);
    webView.clearCache(true);
    webView.reload();
    webView.getSettings().setJavaScriptEnabled(false);
    webView.getSettings().setLoadsImagesAutomatically(true);
        webView.loadUrl("https://www,mydomaine.com");
    swipe.setRefreshing(true);
    webView.setWebViewClient(new WebViewClient() {


        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            webView.loadUrl("file:///android_asset/www/error.html");
        }

        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            //Hide the SwipeReefreshLayout

            swipe.setRefreshing(false);

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

【问题讨论】:

  • 有什么帮助吗????

标签: java android android-webview android-network-security-config


【解决方案1】:

在 android 9 中禁用了明文 所以你必须添加网络安全

添加 src\main\res\xml\network_security_config.xml,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

然后通过设置android:networkSecurityConfig属性在AndroidManifest.xml中注册策略文件。

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

【讨论】:

  • 在这里工作。谢谢。将被检查为正确答案。但是请注意,它需要 Min SDK version >= 24 才能工作。
【解决方案2】:

将此添加到应用程序标签内的清单中

android:usesCleartextTraffic="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多