問題描述:
安卓模擬器使用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