Android9.0 http網絡請求適配方案

背景: 由於 Android P 限制了明文流量的網絡請求,非加密的流量請求都會被系統禁止掉。

場景: 如果當前應用的請求是 htttp 請求,而非 https ,這樣就會導系統禁止當前應用進行該請求,如果 WebView 的 url 用 http 協議,同樣會出現加載失敗,https 不受影響。

解決方案1:

1:在 res 下新建一個 xml 目錄,然後創建一個名爲:network_security_config.xml 文件 ,該文件內容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

2.然後在 AndroidManifest.xml application 標籤內應用上面的xml配置:

 <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
        android:networkSecurityConfig="@xml/network_security_config"
        >

方案2:

targetSdkVersion 降級回到 27(包含27)

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