Android WebView failed to load (net::ERR_CLEARTEXT_NOT_PERMITTED)

ぃ、小莉子 提交于 2020-03-19 22:58:14

问题


Can someone help me please? I am using WebView in my Android app

compileSdkVersion 29
buildToolsVersion "29.0.0"
minSdkVersion 16
targetSdkVersion 29

I have the config https in AmdroidManifest and creating a config file but no change I get the cleartext error :

(net::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 Class

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

回答1:


cleartext is disabled in android 9 so you must add network security

Add src\main\res\xml\network_security_config.xml with the following content:

    <?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>

Then register policy file in AndroidManifest.xml by setting the android:networkSecurityConfig attribute.

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


来源:https://stackoverflow.com/questions/56820089/android-webview-failed-to-load-neterr-cleartext-not-permitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!