安卓9.0/P及以上版本無法直接訪問未加密http
我在本地配置的後端用的是http而不是https,結果無法訪問,查閱資料後發現Android P 限制了明文流量的網絡請求,非加密的流量請求都會被系統禁止掉。
解決方法:
參考自:Android 9.0/P okhttp網絡請求出錯
在res目錄下新建一個xml目錄,在xml目錄下新建network_security_config.xml文件, 插入以下代碼
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
之後在AndroidManifest.xml文件的appliction標籤中加入:
android:networkSecurityConfig="@xml/network_security_config"
加入後如下:
<application
android:networkSecurityConfig="@xml/network_security_config"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
AVD模擬器訪問網絡時報錯 java.net.SocketException(socket failed: EPERM (Operation not permitted)),真機可以
使用Glide在獲取網絡圖片時出錯,之後在AndroidManifest.xml文件中加入
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
並***卸載軟件重裝***後問題解決。