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

React Native中Apollo GraphQL Query组件安卓真机持续加载问题

React Native Apollo Query组件在真实安卓设备加载失败问题排查
  • 检查网络权限与明文请求限制
    真实安卓设备可能因网络配置异常导致请求失败:

    1. 确保AndroidManifest.xml中添加了必要网络权限:
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      
    2. 若你的GraphQL服务部署在本地IP(如你提供的25.00.489.69)且使用HTTP协议,安卓9及以上默认禁止明文请求,需创建android/app/src/main/res/xml/network_security_config.xml:
      <?xml version="1.0" encoding="utf-8"?>
      <network-security-config>
          <domain-config cleartextTrafficPermitted="true">
              <domain includeSubdomains="true">25.00.489.69</domain>
          </domain-config>
      </network-security-config>
      
      并在AndroidManifest.xml的application标签中引用该配置:
      android:networkSecurityConfig="@xml/network_security_config"
      
    3. 确认真实设备与服务端处于同一局域网,且能正常ping通目标IP。
  • 修正代码语法与规范问题
    你的代码存在几处可能导致真实设备解析异常的问题:

    1. fetchpolicy应为驼峰式的fetchPolicy,这是Apollo Client的标准属性名;
    2. JSX组件名需大写(BrandList、Text),属性名也需遵循驼峰规范(scrollIntoView、allowFontScaling);
      修正后的代码示例:
    <Query 
      query={GET_BRANDS} 
      fetchPolicy="network-only" 
      client={xxx}
    >
      {({data, loading, error}) => { 
        if(data && data.getBrand){ 
          return <BrandList value={data.getBrand} navigation={this.props.navigation} scrollIntoView={this.props.scrollIntoView}/> 
        } else if (error) {
          return <Text allowFontScaling={false}>{JSON.stringify(error)}</Text>
        } else if (loading) {
          return <Text allowFontScaling={false}>Loading...</Text>
        } 
        return null 
      }}
    </Query>
    
  • 排查具体错误原因
    在error分支中添加日志输出,比如console.error('Apollo error:', error),然后通过adb logcat命令查看安卓设备的详细日志,定位是请求超时、DNS解析失败还是服务端返回异常。

  • 验证Apollo Client配置
    确认Apollo Client的链接endpoint在真实设备上可正常访问,排查是否有请求拦截中间件(如缓存、认证)在真实设备环境下触发异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 19:24:10