Netcob
Netcob

Reputation: 31

How do I sync Couchbase-Lite Android with CouchDB?

The problem: During a push I get a "Bad Request" error and some sort of authentication error even though it gets a positive response.

I have almost no experience with CouchDB or Couchbase, but from what I understand you'd usually sync Couchbase-Lite with Couchbase Sync Gateway, but since the latter uses CouchDB's replication protocol you can still use CouchDB as long as you don't use "Channels". Replication with CouchDB is mentioned here. I would prefer not to use Couchbase Sync and Couchbase Server since this is a small experimental project for now, I don't need channels and my server has nowhere near enough resources.

I'm using CBL-Android 0.0.0-501 (the most recent one right now I think, already tried 1.0.3) and CouchDB 1.5.0.

This is called in the onCreate method of my main activity on Android:

void minimalPushTest() throws IOException, CouchbaseLiteException {
    String databaseName = "cblpushtest";
    String couchDbUrl = "http://192.168.4.11:5984";
    String userName = "testuser";
    String userPw = "testpw";

    // initialize database
    Manager manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
    Database database = manager.getDatabase(databaseName);
    Document document = database.createDocument();
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("somekey", "sometext");
    document.putProperties(data);

    // start push replication
    Replication replication = database.createPushReplication(new URL(couchDbUrl));
    replication.setContinuous(false);
    replication.setAuthenticator(new BasicAuthenticator(userName, userPw));
    replication.start();
}

This is what CouchDB says about "/_session" for user "testuser":

{"ok":true,"name":"testuser","roles":["testing"]}

...and for "/cblpushtest/_security":

{"admins":{"names":[],"roles":[]},"members":{"names":["testuser"],"roles":["testing"]}}

Here is the logcat output in Android Studio:

03-15 23:15:14.999  27429-27429/ W/Sync﹕ [fireTrigger()] => START
03-15 23:15:15.109  27429-27429/ W/Sync﹕ [fireTrigger()] => GO_ONLINE
03-15 23:15:15.199  27429-27546/ E/Sync﹕ com.couchbase.lite.replicator.ReplicationInternal$4@429cbb20 checkSessionAtPath() response: {ok=true, userCtx={name=testuser, roles=[testing]}, info={authentication_db=_users, authentication_handlers=[oauth, cookie, default], authenticated=default}}
03-15 23:15:15.309  27429-27546/ W/Sync﹕ com.couchbase.lite.replicator.ReplicationInternal$9@4291cab0: error getting remote checkpoint
03-15 23:15:15.309  27429-27546/ E/Sync﹕ com.couchbase.lite.replicator.PusherInternal@429c1758: Progress: set error = org.apache.http.client.HttpResponseException: Bad Request
03-15 23:15:15.309  27429-27546/ W/Sync﹕ [fireTrigger()] => STOP_GRACEFUL
03-15 23:15:15.314  27429-27562/ W/Sync﹕ [fireTrigger()] => WAITING_FOR_CHANGES
03-15 23:15:15.314  27429-27562/ W/Sync﹕ [fireTrigger()] => STOP_IMMEDIATE

Somehow an "ok=true" response is shown as an error.

And this is couch.log (clocks aren't seem synchronized, but these are the relevant outputs):

[Sun, 15 Mar 2015 22:14:32 GMT] [info] [<0.29026.2>] 192.168.4.61 - - GET /_session 200
[Sun, 15 Mar 2015 22:14:32 GMT] [info] [<0.29024.2>] 192.168.4.61 - - GET /_local/3e7d908842481392245906438560f235e9d3138f 400

Upvotes: 1

Views: 1536

Answers (1)

Netcob
Netcob

Reputation: 31

Figured it out: the database url needs to point to the actual database, not just to the CouchDB root. I thought I had tested that earlier, but I think at that time I still had authentication issues so I had dismissed that solution too early.

The correct line for couchDbUrl is therefore

String couchDbUrl = "http://192.168.4.11:5984/"+databaseName;

..and it probably doesn't even need to have the same name, but I didn't check. Anyway, the data gets pushed now.

Upvotes: 2

Related Questions