NativeScript HTTP GET请求失效,疑与Android版本有关?
It sounds like you’re hitting a common Android version-specific restriction here. Let’s break down the most likely causes and fixes step by step:
1. Android 9’s Cleartext HTTP Blocking (Most Probable Cause)
Starting with Android 9 (API level 28), Google blocks cleartext HTTP traffic by default—only HTTPS is allowed unless explicitly permitted. Since your backend uses http:// instead of https://, this explains why the Android 8.1 emulator (which doesn’t enforce this rule) works, but the Android 9 device doesn’t.
Fix: Allow Cleartext Traffic in AndroidManifest.xml
Add the android:usesCleartextTraffic="true" attribute to your <application> tag in AndroidManifest.xml:
<application ... android:usesCleartextTraffic="true"> ... </application>
For production, you might want to use a network security config file for granular control over allowed domains, but this change will get you up and running for testing quickly.
2. Ensure Network Permissions Are Explicitly Declared
Double-check that your app has the required network permissions in the manifest. Even if some frameworks auto-add these, explicit declarations avoid unexpected issues:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Optional but helpful for network status checks -->
3. Debug the Error Callback (Critical for Diagnostics)
Your current code has an empty error handler (e => {}), which swallows all error messages that could tell you exactly what’s failing. Update the callback to log errors:
getTest() { console.log('test') httpModule .request({ url: "http://XXX.XXX.XXX.XXX:8080/XXX/XXX", method: "GET" }) .then( response => { const str = response.content.toString(); const head = response.headers.toString(); console.log("Content:" + str + " Header: " + head ); }, e => { // Log detailed error information console.error("Request failed:", e.message || JSON.stringify(e)); }); }
This will output specific errors (like "Cleartext HTTP not permitted") that confirm the root cause.
4. Check Device-Specific Network Settings
Samsung Galaxy Tab Active2 might have extra security features blocking traffic:
- Same Network Verification: Make sure the tablet is connected to the exact same Wi-Fi network as your Node.js backend (since you’re using a local IP address).
- Proxy Settings: Go to Settings > Network > Wi-Fi > Long-press your network > Modify network > Verify no proxy is configured—proxies can block access to local backend IPs.
- Samsung Security Tools: Devices with Knox or pre-installed security apps might restrict network access. Temporarily disable these for testing.
5. Disable Battery Optimization
Android 9’s battery optimization might put your app in a background state that blocks network requests. To disable it:
- Go to Settings > Battery > Battery Optimization.
- Find your app in the list.
- Select "Don’t optimize" and confirm.
Try these steps in order—starting with enabling cleartext traffic and debugging the error callback should quickly point you to the solution.
内容的提问来源于stack exchange,提问作者Ifloop

