gpt4 book ai didi

android - react native assembleRelease 无法使用 axios 获取数据

转载 作者:行者123 更新时间:2023-11-29 13:55:52 26 4
gpt4 key购买 nike

我有一个实时工作的 restful API 返回使用 nodeJS 创建并上传到网络服务器的 JSON 数据。此外,我正在使用 react native 为 android 和 IOS 构建前端。当我使用“react-native run-android”在 android 中构建应用程序的调试版本时,api 工作正常并按预期返回数据。但是当我使用“cd android && ./gradlew assembleRelease”在 android 中构建发布版本时,该应用程序似乎无法从 API 获取数据。我在我的应用程序中看不到节点服务器中的任何命中。我正在为我的 http 请求使用 axios。

最佳答案

我的问题已经解决了。根据 Google 的官方文档,只有具有有效 SSL 证书的安全域才能向 Web 服务器发出请求。要强制来自不安全域或具有自签名证书的域的 HTTP 请求,只需在

"yourProject/android/app/src/main/res/xml/network_security_config.xml"

内容如下-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

<domain-config cleartextTrafficPermitted="true">

<!-- For React Native Hot-reloading system -->
<!-- If you are running on a device insert your computer IP -->
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">your own domain ip</domain>

<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>

</domain-config>

<base-config cleartextTrafficPermitted="false" />

</network-security-config>

然后通过将此行添加到应用程序标记之间的 AndroidManifest.xml 来导入 xml 文件-

<application...
android:networkSecurityConfig="@xml/network_security_config"
...>

关于android - react native assembleRelease 无法使用 axios 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com