React Native生成的APK在Android 9设备无法发起服务器请求
Hey there, let's figure out why your React Native app's axios GET request isn't reaching your server on the Galaxy S8 (Android 9) — especially since it works perfectly on older Android versions and simulators. Here are the most common issues and fixes to try out:
1. Android 9's Default Cleartext Traffic Block
Android 9 (API level 28) disables unencrypted HTTP requests by default. If your server uses http:// instead of https://, the system will silently block the request without throwing an obvious error, which matches your "loading forever" scenario.
- Quick fix: Add
android:usesCleartextTraffic="true"to the<application>tag in yourandroid/app/src/main/AndroidManifest.xml. - Best practice: If possible, switch your server to HTTPS — this is more secure and avoids needing this workaround long-term.
2. Granular Network Security Configuration
If you don't want to allow cleartext traffic globally, you can whitelist specific domains:
- Create a file at
android/app/src/main/res/xml/network_security_config.xmlwith this content:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">your-server-domain-or-ip.com</domain> </domain-config> </network-security-config> - Link this config to your app by adding
android:networkSecurityConfig="@xml/network_security_config"to the<application>tag inAndroidManifest.xml.
3. Incorrect Axios Base URL
Double-check if your axios base URL uses localhost or 127.0.0.1. Simulators map this to your development machine, but real devices can't access your local server via these addresses unless:
- Your phone and development machine are on the same WiFi network, and you use your machine's local LAN IP (e.g.,
http://192.168.1.100:3000) as the base URL. - For production builds, use your server's public domain or IP instead of local addresses.
4. ProGuard Obfuscation Breaking Network Code
If you're building a release APK with ProGuard enabled, it might be obfuscating axios or underlying network libraries (like OkHttp), causing requests to fail silently.
Add these rules to android/app/proguard-rules.pro to protect critical network code:
-keep class com.facebook.react.modules.network.** { *; } -keep class okhttp3.** { *; } -keep class okio.** { *; } -keep class com.google.gson.** { *; }
Then rebuild your release APK.
5. Missing Network Permissions
While older devices work, it's worth confirming your AndroidManifest.xml includes the required network permissions:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Note: INTERNET is a normal permission, so you don't need to request it dynamically on Android 6+.
6. Add Error Logging to Diagnose Exact Issues
Right now, you're flying blind without error details. Wrap your axios request in a catch block to log errors, and use adb logcat to view device logs:
axios.get('/your-api-endpoint') .then(response => { // Handle successful response }) .catch(error => { console.error('Request failed:', error.message); console.error('Full error details:', error); // Optionally display the error on-screen for easier testing });
This will tell you if the issue is SSL-related, a network timeout, or something else entirely.
内容的提问来源于stack exchange,提问作者sajid

