WebView无法加载页面报错 net:ERR_CLEARTEXT_NOT_PERMITTED 还有webView加载网页后出现ERR_UNKNOWN_URL_SCHEME
根据网络安全配置-
从Android 9(API级别28)开始,默认情况下禁用明文支持。因此http的url均无法在webview中加载
还可以看看-https: //koz.io/android-m-and-the-war-on-cleartext-traffic/
Codelabs解释- https://codelabs.developers.google.com/codelabs/android-network-security-config/index.html
如果应用使用的是非加密的明文流量的http请求,则会导致该请求无法成功,而https请求则不会受影响。
针对这个问题,我看了一下共有三种解决方法。
1)使用Https请求替换Http请求。既然官方都说了用Https更安全,那就替换成Https。
2)将targetSdkVersion降到27以下。曲线救国,既然从28开始不支持Http,那么我就用28以下的版本。
3)更改网络安全配置
第一种与第三种是网上比较多多的解决方案,一个就是使用更安全的https,另一个方法就是在清单文件中做一下配置,使用未加密信息传输。
<application
....
android:usesCleartextTraffic="true"
....>
....
</application>
解决办法:根本上选项一即可,其他解决办法是在不得已的情况下
选项1 -
从根本解决问题,用安全的HTTPS协议 eg: "https://" 替换 "http://"
首先尝试用“ https://”而不是“ http://”打入URL
选项2-
创建文件res / xml / network_security_config.xml-
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
在application节点添加 android:networkSecurityConfig="@xml/network_security_config" 引用
AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
选项3-
android:usesCleartextTraffic Doc
在 AndroidManifest.xml 文件中的application节点添加 android:usesCleartextTraffic="true"。
AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
正如@ david.s的答案所指出的,这android:targetSandboxVersion
也可能是一个问题-
根据清单文件-
android:targetSandboxVersion
此应用要使用的目标沙箱。沙盒版本号越高,安全级别越高。默认值为1;默认值为1。您也可以将其设置为2。将此属性设置为2会将应用程序切换到其他SELinux沙箱。以下限制适用于2级沙箱:
usesCleartextTraffic
网络安全配置中的默认值为false。- 不允许共享Uid。
所以选项4-
如果有android:targetSandboxVersion
,<manifest>
则减少到1
AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
-
在最新的P版本中似乎还必须再迈出一步?请参阅我的问题stackoverflow.com/questions/51770323/…– spartygw
-
将选项1从domain-config更改为base-config可以解决此问题针对此问题的最佳解决方案:使用HTTPS。此答案中提到的选项只能是不得已的方法。
(选项二)第二种办法
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
和:
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>
(选项二)第三种方法
<application
....
android:usesCleartextTraffic="true"
....>
更新:如果您具有网络安全性配置,例如:android:networkSecurityConfig="@xml/network_security_config"
如上所示,无需将明文流量设置为true,而使用以下代码:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
....
....
</domain-config>
<base-config cleartextTrafficPermitted="false"/>
</network-security-config>
以上是开发中遇到的问题,后期有时间还会继续整理,或者您有更好的解决方法,添加更加完善,不胜感激。欢迎留言交流,
书山有路勤为径,学海无涯苦作舟!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战