You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo应用API集成报错:TypeError: Network request failed

解决React Native Expo中[TypeError: Network request failed]问题

常见原因及修复方案

  • 协议限制问题
    网页端可兼容HTTP/HTTPS,但Expo移动应用受iOS ATS、Android网络安全规则约束,默认禁止HTTP请求:

    • 优先将API切换为HTTPS
    • 若必须用HTTP,在app.json中配置:
      {
        "ios": {
          "infoPlist": {
            "NSAppTransportSecurity": {
              "NSAllowsArbitraryLoads": true
            }
          }
        },
        "android": {
          "networkSecurityConfig": {
            "domainConfigs": {
              "example.com": {
                "cleartextPermitted": true
              }
            }
          }
        }
      }
      
  • API地址错误
    不要用localhost/127.0.0.1,移动设备的localhost指向自身而非开发电脑。改用电脑局域网IP(如192.168.1.100)或公网API地址,同时确认防火墙未拦截对应端口。

  • 网络权限缺失
    确认app.json中Android权限包含INTERNET(Expo默认已添加,可自行检查):

    {
      "android": {
        "permissions": ["INTERNET"]
      }
    }
    
  • API服务器限制
    部分API服务器会针对请求来源做限制,即使React Native不受浏览器CORS约束,若服务器仅允许特定网页域名访问,仍会拒绝请求。需修改服务器配置,允许任意来源(Access-Control-Allow-Origin: *)或适配移动请求标识。

调试技巧

  • 打印完整请求URL,校验地址正确性
  • 用手机浏览器直接访问API地址,确认服务正常
  • 使用Expo Network模块检查网络连接状态:
    import * as Network from 'expo-network';
    
    const checkNetworkStatus = async () => {
      const { isConnected } = await Network.getNetworkStateAsync();
      console.log('当前网络连接状态:', isConnected);
    };
    

内容的提问来源于stack exchange,提问作者Surendhar S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 14:42:46