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

NativeScript HTTP GET请求失效,疑与Android版本有关?

Troubleshooting GET Request Failure on Android 9 (Samsung Galaxy Tab Active2)

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:

  1. Go to Settings > Battery > Battery Optimization.
  2. Find your app in the list.
  3. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:57:33