Android 9.0 WebView无法加载页面报错 net:ERR_CLEARTEXT_NOT_PERMITTED

有时候需要把一个网址打包成一个APP,会遇到一些问题。

       看到了就记录一下,方便以后使用:

 

net:ERR_CLEARTEXT_NOT_PERMITTED报错原因是:
从Android 9.0(API级别28)开始,默认情况下禁用明文支持。因此 http 的url均无法在webview中加载。

仔细一看我的正是http的网关协议。

解决办法:

1、从根本解决问题,用安全的HTTPS协议  eg: "https://" 替换 "http://"


2、在 AndroidManifest.xml 文件中的application节点添加 android:usesCleartextTraffic="true"

3、创建 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" 引用

 

4、如果你有android:targetSandboxVersion在<manifest>,然后减少到1</manifest>

最后再啰嗦一句,记得给网络权限 [微笑脸]。

 

参考链接: Android 8: Cleartext HTTP traffic not permitted

 

书到用时方恨少,纸上得来终觉浅。共勉。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章