安卓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"/>
并***卸载软件重装***后问题解决。
安卓9.0/P及以上版本因系统限制无法直接访问http,需要创建network_security_config.xml配置明文流量。AVD模拟器出现SocketException,通过在AndroidManifest.xml中设置网络配置并卸载重装应用解决。

4545

被折叠的 条评论
为什么被折叠?



