美文网首页
React Native 项目运行在安卓模拟器Http请求报错

React Native 项目运行在安卓模拟器Http请求报错

作者: jinrui_w | 来源:发表于2019-03-21 09:20 被阅读0次

问题描述:
安卓模拟器使用http请求报错TypeError: Network request failed,但是https可以正常使用

image

解决办法:
找到react_native_config.xml文件(路径:rn项目中android/app/src/debug/res/xml),此文件中内容是这样的:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="false">localhost</domain>
    <domain includeSubdomains="false">10.0.2.2</domain>
    <domain includeSubdomains="false">10.0.3.2</domain>
  </domain-config>
</network-security-config>

方法一: 添加我们自己的域名(可以处理已添加域名的http请求)

<domain includeSubdomains="false">自己的域名</domain>

方法二: 修改为如下内容(可以处理所有的http请求)

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

注意: 修改完此文件后需要保存,并使用react-native run-android重新运行,否则不生效

参考地址:
https://blog.csdn.net/qq_18620851/article/details/80617549

相关文章

网友评论

      本文标题:React Native 项目运行在安卓模拟器Http请求报错

      本文链接:https://www.haomeiwen.com/subject/pvmfvqtx.html