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

Android应用连接Couchbase Sync Gateway URL错误求助

Fixing Couchbase Sync Gateway Connection Failures (java.net.ConnectException) on Android

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:4984 with 0.0.0.0:4984 if 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).

  1. Open Command Prompt on Windows.
  2. Run ipconfig and look for the IPv4 Address under 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:

  1. Connect your phone to the same WiFi network as your Windows machine.
  2. 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).

Step 5: Double-Check Common Mistakes

  • Port mix-up: Sync Gateway uses 4984 for public client access and 4985 for administrative tasks. Make sure you're using 4984 in your app.
  • Emulator vs. Physical Device: localhost works 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:42:20