在 android 中调用 api http 失败

Failed call api http in android

我在 android 中使用 API 本地主机,但无法从 android 调用它。我已将 localhost 更改为 IP,但也无法调用它。我一直在使用另一个 API 并且它是成功的。我认为这是因为http连接。

最后我用这个reference. I also get some answer from

您需要定义 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>