在http网络请求时,react-native或者Android报错:Network request failed时

 

原因:android9.0以上由于安全性高了,所有不做处理http不能使用,解决方法有如下三种

1.降级targetSdkVersion的版本,降到27或27以下

2.服务器请求http改为https (推荐)

3.兼容http请求

# 在清单文件中设置networkSecurityConfig属性 
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config">
    </application>
</manifest>
 
# 在res/xml文件夹下创建network_security_config.xml

<?xml version ="1.0" encoding ="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
      <trust-anchors>
        <certificates src="system" />
      </trust-anchors>
    </base-config>
</network-security-config>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐