1.記錄一次在使用http請(qǐng)求時(shí)出現(xiàn)的錯(cuò)誤:
CLEARTEXT communication to 192.168.31.236 not permitted by network security
2.查閱資料才知道 在Android P(API 28)系統(tǒng)的設(shè)備上辐赞,默認(rèn)要求使用加密連接。譬如搂根,默認(rèn)只能使用HTTPS進(jìn)行通信珍促,若強(qiáng)行使用HTTP通信,則會(huì)得到上面的異常剩愧。
3.解決辦法:
1).在res目錄下創(chuàng)建一個(gè)xml文件夾猪叙,然后新建配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"></base-config>
</network-security-config>
2).在清單文件中配置 Application networkSecurityConfig 屬性
<application
android:networkSecurityConfig="@xml/network_security_config"
...................