Android 9(P)

从 Android 9.0 系统开始,应用程序默认只允许使用 HTTPS 类型的网络请求,HTTP 类型的网络请求因为有安全隐患默认不再被支持,而这里测试使用的是 HTTP,为了让程序使用 HTTP,还需进行配置。

在 res -> xml 目录下新建 network_config.xml 文件:这段配置文件的意思就是允许以明文的方式在网络上传输数据,而 HTTP 使用的就是明文传输方式。

1
2
3
4
5
6
7
8
<?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>

接下来修改 AndroidManifest.xml 中的代码来启用刚才创建的配置文件。

1
2
3
4
5
6
7
8
9
<application
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"
android:networkSecurityConfig="@xml/network_config">
</application>

备注

欢迎关注微信公众号:非也缘也