Search code examples
androidsynchronizationcouchbasecouchbase-litecouchbase-sync-gateway

Sync android application to couchbase server on localhost


I am trying to connect my android app with couchbase server on localhost I have initialized database and manager as follows: after initializing database i am calling startSync() function accepting parameter of type 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;
                        }

                    }

This is the startSync() function

         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();

                }

but gettin errors my logcat is as follows: I am confused about using url in android app

    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.access$201(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)

These errors occures when i run the application on android device.when i run my application on emulator it shows nothing in error but gives message in verbose

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=}]}

Solution

  • I got solution for my problem mentioned above with discussing with friends I just connected my Laptop and Android phone to the same network(It is important while accessing the server form localhost) then i changed the url in my startSync() function as follows:

    http://192.168.1.17:4984/firstdb

    192.168.1.17

    This is the IP Address of my laptop and 4984 is port listening to request on sync_gateway Just making to these simple changes to above code you will able to use sync_gateway in android application