从Android 9.0(API级别28)开始,不能直接使用http访问服务器地址。
解决方法:
一、在android studio的项目目录的src/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配置文件中的application中添加如下配置项
android:networkSecurityConfig="@xml/network_security_config"
示例如下:
<application
android:allowBackup=""
android:icon=""
android:label=""
android:roundIcon=""
android:supportsRtl=""
android:theme=""
android:networkSecurityConfig="@xml/network_security_config">
注意点
只要app涉及联网功能,都要在AndroidManifest.xml中添加如下配置项
<uses-permission android:name="android.permission.INTERNET" />
参考:
https://blog.csdn.net/a497785609/article/details/104731719/https://blog.csdn.net/a497785609/article/details/104731719/https://www.cnblogs.com/liu9539/p/12080472.html
https://www.cnblogs.com/liu9539/p/12080472.html