Android 9.0 網(wǎng)絡(luò)適配

Android 9.0要求使用安全的網(wǎng)絡(luò)訪問,如果使用http請求會(huì)報(bào)錯(cuò)。
解決方案:
在 res/ xml 目錄下新增一個(gè)network_security_config.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<!--Android 9.0 https請求適配-->
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在APP的AndroidManifest.xml文件下的application標(biāo)簽增加以下屬性:

android:networkSecurityConfig="@xml/network_security_config"

上一個(gè) xml 的配置只是解決了項(xiàng)目中自己的接口請求使用http的問題,如果有第三方的http請求需要在 xml 文件中設(shè)置相關(guān)路徑:

<?xml version="1.0" encoding="utf-8"?>
<!--Android 9.0 https請求適配-->
<network-security-config>
    <!--<base-config cleartextTrafficPermitted="true" />-->
        <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">你的服務(wù)端接口地址</domain>
        <domain includeSubdomains="true">第三方接口路徑</domain>
    </domain-config>
</network-security-config>

需要注意的是接口地址不要加 http:// 前綴,如果是 ip 地址,還不能加端口號(hào)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容