【问题标题】:Failed call api http in android在android中调用api http失败
【发布时间】:2019-11-16 12:56:18
【问题描述】:
我在 android 中使用 API localhost,但无法从 android 调用它。我已将 localhost 更改为 IP 但也无法调用它。我一直在使用另一个 API,它是成功的。我认为是因为 http 连接。
【问题讨论】:
标签:
android
api
spring-boot
android-studio
retrofit2
【解决方案1】:
最后我使用了这个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>
那个network_security_config.xml也需要在Manifest.xml中引用:
<?xml version="1.0" encoding="utf-8"?>
<manifest
...>
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>