因为从Android9.0(API 28)开始,NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(), 将return false。这表示Android默认将禁止明文访问网络,只允许使用https url访问
可以有以下途径强制启用http连接
1. AndroidManifest文件的application节点添加android:usesCleartextTraffic="true"
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
2.AndroidManifest文件的application节点添加
android:networkSecurityConfig="@xml/network_security_config"
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
并新建res/xml/network_security_config.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
</domain-config>
</network-security-config>
3.在AndroidManifest文件的manifest节点添加android:targetSandboxVersion="1"
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>