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

Android与Node.js跨网络异步通信失败问题求助

Hey there! Let’s work through your issue step by step—you’ve got a couple of key things to fix here, both with your server setup and network accessibility.

First: Fix Your Server's CORS Configuration

You’ve installed the cors package but aren’t actually using it in your Express app, which is definitely causing cross-origin request blocks. Plus, your POST endpoint isn’t properly parsing JSON data. Here’s the corrected server code:

var express = require('express'); 
var app = express(); 
var port = 9000; 
var cors = require('cors'); 
var welcome = "new msg from server!";

// Enable CORS for all requests (safe for development; restrict to specific domains in production)
app.use(cors());

// Add middleware to automatically parse JSON request bodies
app.use(express.json());

app.get('/get', function(req, res) { 
  console.log('>> send new msg to client...'); 
  res.json(welcome); 
}) 

app.post('/post', function(req, res) { 
  console.log('<< receive new msg from client...'); 
  // Access parsed JSON directly from req.body (no need for manual data event handling)
  var info_from_client = req.body;
  var log_temp = `contents : 
name = ${info_from_client.user_name}
finger_print = ${info_from_client.finger_print}`; 
  console.log(log_temp); 
  res.send(log_temp); // Simplified response method
}) 

app.listen(port, function() { 
  console.log(`now server is starting on port ${port}...`); 
}) 

Second: Fix Mobile Data Network Accessibility

Your setup works on WiFi because your Android device and server are on the same local network. When switching to mobile data, your server is hidden behind your home network’s NAT (Network Address Translation)—mobile networks can’t directly reach your local server IP. You have two solid fixes for this:

  • Option 1: Port Forwarding on Your Router

    1. Log into your router’s admin panel (usually at 192.168.1.1 or similar)
    2. Find the "Port Forwarding" or "Virtual Server" section
    3. Map your server’s local IP (e.g., 192.168.xxx.xxx) and port 9000 to your public IP’s port 9000
    4. Update your Android client to use your public IP (search "my public IP" to find it) instead of the local IP

    Note: Some ISPs block non-standard ports like 9000—if this happens, try switching to port 8080 or 80.

  • Option 2: Use an Intranet Penetration Tool
    If you don’t have a public IP or don’t want to mess with router settings, use a tool like ngrok or frp to expose your local server to the public internet. For ngrok, just run:

    ngrok http 9000
    

    It’ll give you a public URL (like https://abc123.ngrok.io)—update your Android client to use this URL instead of your local IP.

Third: Small Android Client Improvements

Tweak your client code for reliability and ensure permissions are set:

  1. Add Timeouts
    Prevent hanging requests by adding timeouts to your HttpURLConnection:
    connection.setConnectTimeout(5000); // 5-second connection timeout
    connection.setReadTimeout(5000); // 5-second read timeout
    
  2. Verify Permissions
    Make sure your AndroidManifest.xml includes these permissions:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
  3. Update Request URL
    Swap out your local WiFi IP for the public IP/ngrok URL when testing on mobile data.

Final Verification Steps

  1. Restart your updated Node.js server
  2. Test the /get endpoint from your mobile data browser first (e.g., http://your-public-ip:9000/get) to confirm connectivity
  3. If still stuck, check your server’s firewall to ensure port 9000 is open

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:41:46