Android Pie不支持http請求的解決方案

        相信很多在android8.0之前很正常,跑到p上數據都無法加載出來,忽然就懵逼了,因爲google在android p爲了安全起見,已經明確規定禁止http協議額,但是之前很多接口都是http協議啊,怎麼解決呢?
   下面介紹四種方法:
   1.使用https,當然這是最好的解決方式,也是比較複雜的,因爲之前很多接口都是http協議的,不可能一個個替換啊,可以留待空餘時間慢慢修改.
   2.target降低至27,這也是一個方法,可以暫解燃眉之急,因爲在target27之前是沒有對http協議做過限制的,但是後期會出現什麼問題,還有未可知.
   3.允許使用http,你可以禁止,但我也可以讓他允許啊,既然前兩個方法都比較差,那我就推薦第三種方法,下面請看:
 在res目錄下創建xml目錄,然後隨便創建一個.xml文件,裏面內容如下:

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

然後在AndroidManifest.xml文件下加上:
android:networkSecurityConfig="@xml/文件名"

<application
    android:testOnly="false"
    android:name=".base.BaseApplication"
    android:allowBackup="true"
    android:configChanges="orientation|keyboardHidden"
    android:networkSecurityConfig="@xml/文件名"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"
    tools:replace="android:icon,android:label,android:theme,android:allowBackup,android:name">

4.在AndroidManifest.xml文件中的application節點中,配置:
android:usesCleartextTraffic="true"

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