我正在android中使用API localhost,但无法从android调用它。我已将localhost更改为IP,但也无法调用它。我一直在使用另一个API,并且成功了。我认为是因为http连接。
最后,我正在使用此reference。我也从this得到了一些答案。
您需要定义一个res / xml / network_security_config.xml并允许该主机使用HTTP:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">server.com</domain>
</domain-config>
</network-security-config>
也需要在Manifest.xml中引用network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
...>
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>