自Android9.0系統開始,應用程序默認只允許使用HTTPS類型的網絡請求,HTTP類型的網絡請求因爲有安全隱患默認不再支持。爲了讓應用程序支持HTTP請求,可以做如下設置:
1、在res/xml目錄下新建xml文件,命名network_config.xml
2、修改新建xml文件內容
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted = "true">
<trust-anchors>
<certificates src="system"/>
</trust-anchors>
</base-config>
</network-security-config>
3、修改Manifest文件以啓動剛纔創建的配置文件:
<application
android:name=".SophixStubApplcation"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="${app_icon}"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:networkSecurityConfig="@xml/network_config">
劃重點:
android:networkSecurityConfig="@xml/network_config"