Android物理设备请求localhost服务器出现connection timeout问题求助
Hey there, let’s work through this frustrating issue together—I’ve dealt with similar headaches testing apps against local servers, so I know how annoying this can be! Here are the most likely fixes to try:
1. Stop Using localhost/127.0.0.1 for Physical Devices
This is the #1 mistake most people make: on a physical Android device, localhost points to the device itself, not your development machine. Instead, you need to use your development computer’s local network IP address (e.g., 192.168.1.105).
To find your IP:
- On Windows: Run
ipconfigin Command Prompt, look for "IPv4 Address" under your active network adapter. - On Mac/Linux: Run
ifconfigorip addrin Terminal, find the IP associated with your Wi-Fi/Ethernet connection.
Replace every instance of localhost in your app’s API calls with this IP (e.g., http://192.168.1.105:8080/api/endpoint).
2. Check Your Development Machine’s Firewall
Firewalls often block incoming requests from other devices on the local network. Try temporarily disabling your firewall to test if that fixes the timeout. If it works, create an inbound rule to allow traffic on your server’s port (e.g., 8080) for local network connections.
3. Fix Cleartext HTTP Restrictions (Android 9+)
Starting with Android 9 (API level 28), apps default to blocking cleartext HTTP requests. If your local server uses HTTP (not HTTPS), this will cause silent failures or timeouts.
Quick Fix:
Add android:usesCleartextTraffic="true" to the <application> tag in your AndroidManifest.xml:
<application ... android:usesCleartextTraffic="true"> ... </application>
More Secure Fix:
Create a network security config file at res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">YOUR_DEV_MACHINE_IP</domain> </domain-config> </network-security-config>
Then reference it in your AndroidManifest.xml:
<application ... android:networkSecurityConfig="@xml/network_security_config"> ... </application>
4. Ensure Your Server is Bound to the Correct Address
Many local servers default to binding only to 127.0.0.1, which means they’ll only respond to requests from the development machine itself. You need to configure your server to listen on 0.0.0.0 (all available network interfaces) or your local network IP.
Examples for common servers:
- Node.js/Express: Use
app.listen(PORT, '0.0.0.0', () => { ... }) - Python Flask: Run with
flask run --host=0.0.0.0 - Django: Use
python manage.py runserver 0.0.0.0:8000
5. Double-Check Network Permissions
You mentioned you’ve added network permissions, but just to be thorough, confirm you have these in your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Final Testing Steps
- Verify your development machine and Android device are on the same Wi-Fi network (critical!).
- Test the server URL from your device’s browser first—if that works, the issue is definitely in your app’s configuration.
- If using an emulator later, remember: Android Studio emulator uses
10.0.2.2to access the host machine’s localhost, while Genymotion uses10.0.3.2.
Give these steps a try—9 times out of 10, one of these fixes will resolve the connection timeout!
内容的提问来源于stack exchange,提问作者Dustin

