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:
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'sfetchdoesn'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.xmland 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:
- Create
android/app/src/main/res/xml/network_security_config.xmlwith this content:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config> - Add this attribute to the
<application>tag in yourAndroidManifest.xml:android:networkSecurityConfig="@xml/network_security_config"
- Create
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-iapthenreact-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

