原因:由于 Android P 限制了明文流量的網(wǎng)絡(luò)請求,非加密的流量請求都會被系統(tǒng)禁止掉。
如果當前應(yīng)用的請求是 htttp 請求,而非 https ,這樣就會導(dǎo)系統(tǒng)禁止當前應(yīng)用進行該請求,如果 WebView 的 url 用 http 協(xié)議,同樣會出現(xiàn)加載失敗,https 不受影響。
為此,OkHttp3 做了檢查,所以如果使用了明文流量,默認情況下,在 Android P 版本 OkHttp3 就拋出異常:?CLEARTEXT communication to " + host + " not permitted by network security policy
解決方法:
1.在 res 下新建一個 xml 目錄,然后創(chuàng)建一個名為:network_security_config.xml 文件 ,該文件內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
????????<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在 AndroidManifest.xml application 標簽內(nèi)應(yīng)用上面的xml配置:android:networkSecurityConfig="@xml/network_security_config"
<application android:name=".app.App"
????android:allowBackup="true"
????android:icon="@mipmap/ic_launcher"
????android:networkSecurityConfig="@xml/network_security_config"
????android:label="@string/app_name"
????android:theme="@style/AppTheme">
2:服務(wù)器和本地應(yīng)用都改用 https?
3:targetSdkVersion 降級回到 27
---------------------
作者:奮斗的小華子
來源:CSDN
原文:https://blog.csdn.net/qq_18620851/article/details/80617549
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!