1

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=}]}
Dhiraj
  • 870
  • 2
  • 12
  • 25
  • Looks like a network issue. Depending on how you run the app, the ip 192.168.1.6 might not be accessible. For instance if you are running the app in the default emulator, the ip of the host is 10.0.2.2. – Laurent Doguin Dec 09 '15 at 17:11
  • @LaurentDoguin i am not running my app in emulator i am running it on android device – Dhiraj Dec 09 '15 at 18:10
  • @LaurentDoguin and i am totally confused about url to be used in android application – Dhiraj Dec 09 '15 at 18:15
  • 1
    If your devices is on the same network than the URL to be used would be one from the machine hosting the sync gateway. – Laurent Doguin Dec 10 '15 at 19:09
  • @LaurentDoguin i did tried it and working nice but what to do if i want to access the machine sync gateway which is in another network? – Dhiraj Dec 11 '15 at 06:51

1 Answers1

0

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

Dhiraj
  • 870
  • 2
  • 12
  • 25