将 android 应用程序同步到本地主机上的 couchbase 服务器

Sync android application to couchbase server on localhost

我正在尝试将我的 android 应用与本地主机上的 couchbase 服务器连接 我已经初始化数据库和管理器如下: 初始化数据库后,我调用 startSync() 函数接受类型为 database

的参数
                  public static final String DB_NAME = "firstdb";
                    public static final String TAG = "firstdb";private void helloCBL() {
                        Manager manager = null;
                        Database database = null;
                        try {
                            manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
                            database = manager.getDatabase(DB_NAME);
                            database.addChangeListener(this);
                            startSync(database);

                        } catch (Exception e) {
                            Log.e(TAG, "Error getting database", e);
                            return;
                        }

                    }

这是 startSync() 函数

         protected void startSync(Database database) {
                        URL syncUrl;
                    try {
                        syncUrl = new URL("http://10.0.2.2:4984/firstdb");
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }


                    Replication pullReplication = database.createPullReplication(syncUrl);
                    pullReplication.setContinuous(true);

                    Replication pushReplication = database.createPushReplication(syncUrl);
                    pushReplication.setContinuous(true);

                    pullReplication.addChangeListener(this);
                    pushReplication.addChangeListener(this);


                    // start both replications
                    pullReplication.start();
                    pushReplication.start();

                }

但是我的 logcat 出现如下错误: 我对在 android app

中使用 url 感到困惑
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest: io exception.  url: http://10.0.2.2:4984/firstdb/_local/2b7e37db76812fef1b8c7ca3d4b30b99e570ea6f
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest: org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:4984 timed out
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at com.couchbase.lite.support.RemoteRequest.executeRequest(RemoteRequest.java:184)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at com.couchbase.lite.support.RemoteRequest.run(RemoteRequest.java:103)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:153)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    12-10 02:36:16.930 4556-4588/com.couchbase.examples.couchdbapp E/RemoteRequest:     at java.lang.Thread.run(Thread.java:856)

当我 运行 在 android device.when 我 运行 我的应用程序时发生这些错误 在模拟器上应用它没有显示任何错误,但给出了详细的消息

12-10 13:13:03.482 3718-3770/com.couchbase.examples.couchdbapp W/Sync: com.couchbase.lite.replicator.PullerInternal@3139637: Received invalid doc ID from _changes: {seq=1, id=_user/, changes=[{rev=}]}

通过与朋友的讨论,我找到了解决上述问题的办法 我刚刚将我的笔记本电脑和 Android phone 连接到同一网络(从本地主机访问服务器时很重要)然后我在 startSync() 函数中更改了 url 如下:

http://192.168.1.17:4984/firstdb

192.168.1.17

这是我笔记本电脑的 IP 地址,4984 是在 sync_gateway 上监听请求的端口只需对上面的代码进行这些简单的更改,您就可以使用sync_gateway 在 android 申请中