Android studio使用webview控件加載加載非https鏈接時(shí)報(bào)錯(cuò)原因。
提示如下內(nèi)容:
Android WebView net::ERR_CLEARTEXT_NOT_PERMITTED
解決方法:
一、問題描述
在Android中使用WebView加載非https鏈接時(shí),出現(xiàn)錯(cuò)誤:
net::ERR_CLEARTEXT_NOT_PERMITTED
二、原因分析
從Android 9.0(API級(jí)別28)開始,默認(rèn)情況下禁用明文支持。因此http的url均無法在webview中加載,Android9.0對(duì)未加密的流量不再信任,添加了新的限制。
三、解決辦法
1.創(chuàng)建文件 res/xml/network_security_config.xml
其中將www.512ons.com替換為你自己的域名,根域名即可,includeSubdomains="true" 就包含了二級(jí)域名。
2.修改 AndroidManifest.xml
添加網(wǎng)絡(luò)訪問權(quán)限:
引入配置文件:
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"