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

React Native安卓发布版获取外部数据报错,Debug版正常求助

Alright, let's break down your two issues here—one related to network requests failing in the release build, another causing a crash from the RNIap module. Let's tackle them one by one:

1. Fixing the Fetch Data Error in Release Build

Your debug build works fine because React Native's dev server handles some network quirks automatically, but release builds use native network stacks with stricter rules. Here's what to adjust:

  • Remove mode: 'no-cors' from your fetch call: This mode is designed for browser environments and makes the response opaque (you can't read any data from it). React Native's fetch doesn't enforce browser-style CORS restrictions, so this flag is unnecessary and breaks your request in release builds. Update your code to:
    let url = 'http://XXX.XXX.XXX.XX:99/app/all'
    fetch(url)
      .then((resp) => resp.json()) // Don't forget to parse the response content!
      .catch((err) => { dispatch(fetchErrText(JSON.stringify(err))) })
    
  • Verify INTERNET permission is enabled: Open android/app/src/main/AndroidManifest.xml and make sure this line is present (it's often added by default, but double-check):
    <uses-permission android:name="android.permission.INTERNET" />
    
  • Allow cleartext HTTP traffic (if your API uses HTTP): Android 9+ blocks unencrypted HTTP requests by default. To fix this:
    1. Create android/app/src/main/res/xml/network_security_config.xml with this content:
      <?xml version="1.0" encoding="utf-8"?>
      <network-security-config>
          <base-config cleartextTrafficPermitted="true" />
      </network-security-config>
      
    2. Add this attribute to the <application> tag in your AndroidManifest.xml:
      android:networkSecurityConfig="@xml/network_security_config"
      
2. Resolving the "Too many receivers" Crash from RNIap

The java.lang.IllegalStateException: Too many receivers error is a clear sign that the RNIap module is registering too many broadcast receivers—hitting the system's 1000-receiver limit. Here's how to fix it:

  • Upgrade react-native-iap to the latest stable version: Older versions of the module had bugs where broadcast receivers were registered repeatedly. Run this command to update:
    npm install react-native-iap@latest --save
    # Or use yarn: yarn add react-native-iap@latest
    
  • Ensure RNIap is initialized only once: Check your code for multiple calls to initConnection(). If you're calling it in multiple components or re-rendering contexts, it will register a new receiver each time. Instead, initialize it once when your app starts, and clean up when the app exits:
    import { initConnection, endConnection } from 'react-native-iap';
    import { useEffect } from 'react';
    
    function App() {
      useEffect(() => {
        const initializeIAP = async () => {
          try {
            await initConnection();
          } catch (error) {
            console.error('Failed to initialize RNIap:', error);
          }
        };
    
        initializeIAP();
    
        // Clean up: Disconnect when the app unmounts to remove receivers
        return () => {
          endConnection();
        };
      }, []);
    
      // Rest of your app code...
    }
    
  • Check for duplicate module links: If you're using an older React Native version (pre-0.60), make sure RNIap isn't linked multiple times. Run react-native unlink react-native-iap then react-native link react-native-iap (auto-linking handles this automatically in newer RN versions).

Start with fixing the RNIap crash first, since that's the immediate cause of your release build failure, then address the fetch issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:52:57