Android应用连接Couchbase Sync Gateway URL错误求助
Let's walk through exactly how to resolve this connection issue—since your local services are running, the root cause is almost certainly a misconfigured URL or network accessibility problem between your physical Android device and Windows machine.
Step 1: Verify Sync Gateway's Binding Address in config.json
First, check that your Sync Gateway is listening on an address your Android device can reach. If it's only bound to 127.0.0.1 (localhost), your phone won't be able to connect.
Open your config.json and look for the interface field:
{ "log": ["*"], "databases": { "your-db-name": { "server": "couchbase://localhost", "bucket": "your-bucket", "sync": "...", "users": { ... } } }, "interface": "0.0.0.0:4984" // Critical: Bind to all network interfaces }
- Replace
127.0.0.1:4984with0.0.0.0:4984if it's present. This lets Sync Gateway accept connections from any device on your local network. - Restart Sync Gateway after making this change.
Step 2: Get Your Windows Machine's Local Network IP
Your Android device needs to target your Windows machine's local IPv4 address (not localhost or 127.0.0.1, which refer to the device itself).
- Open Command Prompt on Windows.
- Run
ipconfigand look for theIPv4 Addressunder your active WiFi/Ethernet adapter (e.g.,192.168.1.105).
Step 3: Update Your Android App's Sync Gateway URL
Replace the incorrect URL in your Android initialization code with your Windows machine's local IP plus the Sync Gateway port (default is 4984).
For example, if your Windows IP is 192.168.1.105 and your database name is my-app-db, your endpoint should look like this:
// Correct URL example URLEndpoint syncEndpoint = new URLEndpoint(URI.create("http://192.168.1.105:4984/my-app-db")); // Incorrect examples to avoid: // new URLEndpoint(URI.create("http://localhost:4984/my-app-db")); // Only works for emulator // new URLEndpoint(URI.create("http://127.0.0.1:4984/my-app-db")); // Same issue as localhost
Step 4: Validate Network Accessibility
Before testing the app, confirm your Android device can reach the Sync Gateway:
- Connect your phone to the same WiFi network as your Windows machine.
- Open the browser on your phone and navigate to
http://[your-windows-ip]:4984(e.g.,http://192.168.1.105:4984).- You should see a JSON response like
{"couchdb":"Welcome","vendor":{"name":"Couchbase Sync Gateway","version":"x.x.x"},"version":"x.x.x"}. - If you don't, check Windows Firewall: Add an inbound rule to allow traffic on port
4984(TCP).
- You should see a JSON response like
Step 5: Double-Check Common Mistakes
- Port mix-up: Sync Gateway uses
4984for public client access and4985for administrative tasks. Make sure you're using4984in your app. - Emulator vs. Physical Device:
localhostworks for Android emulators (via port forwarding), but physical devices need the actual local IP. - Sync Gateway Restart: Always restart Sync Gateway after changing
config.json—changes won't take effect until you do.
If you follow these steps, your app should successfully connect to the Sync Gateway without the java.net.ConnectException error.
内容的提问来源于stack exchange,提问作者user8140661

