React Native 項目運行在安卓模擬器Http請求報錯

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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容