Android WebView加载失败(net :: ERR_CLEARTEXT_NOT_PERMITTED) [英] Android WebView failed to load (net::ERR_CLEARTEXT_NOT_PERMITTED)

查看:1176
本文介绍了Android WebView加载失败(net :: ERR_CLEARTEXT_NOT_PERMITTED)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

compileSdkVersion 29
buildToolsVersion "29.0.0"
minSdkVersion 16
targetSdkVersion 29

我在AmdroidManifest中拥有配置https,并创建了一个配置文件,但是没有任何变化,但我得到了明文错误:

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)

(net::ERR_CLEARTEXT_NOT_PERMITTED)

@ xml/network_security_config

@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

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

推荐答案

明文在android 9中被禁用 所以必须增加网络安全性

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

添加具有以下内容的src \ main \ res \ xml \ network_security_config.xml:

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>

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

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

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

这篇关于Android WebView加载失败(net :: ERR_CLEARTEXT_NOT_PERMITTED)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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