Sync Gateway not working with android studio but working fine with rest API

72 views
Skip to first unread message

Yagyank Chadha

unread,
May 3, 2016, 12:45:01 PM5/3/16
to Couchbase Mobile
I am trying to use CouchBase Lite for my mobile app. I have seen in tutorial of couchbase but it shows how to add sync URL for walrus server not couchbase server. How can I set up sync URL for couchbase server.

Following is the code which I am using but unable to put or get data from couchbase server

`    private URL createSyncURL(boolean isEncrypted){
        URL syncURL = null;
        String host = "127.0.0.1";
        String port = "8091";
        bucketName = "sync_gateway";
        try {
            syncURL = new URL(host + ":" + port + "/" + bucketName);
        } catch (MalformedURLException me) {
            me.printStackTrace();
        }
        return syncURL;
    }`

Below is my Sync gateway conf.json

    {
        "log":["CRUD+", "REST+", "Changes+", "Attach+"],
        "interface":":4986",
       "adminInterface":":14985",
        "databases": {
        "sync_gateway": {
            "server":"http://localhost:8091",
        "bucket":"sync_gateway",
            "sync":`
                function (doc) {
                channel (doc.channels);
            }`,
            "users": {
                "GUEST": {
                    "disabled": false,
                    "admin_channels": ["*"]
                }
            }
        }
        }
    }

and Below is the main activity class of android


    package com.couchbase.examples.couchbaseevents;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import android.util.Log;

    import com.couchbase.lite.CouchbaseLiteException;
    import com.couchbase.lite.Database;
    import com.couchbase.lite.Document;
    import com.couchbase.lite.Manager;
    import com.couchbase.lite.android.AndroidContext;
    import com.couchbase.lite.replicator.Replication;

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;

    public class MainActivity extends AppCompatActivity {

        public static final String DB_NAME = "couchbaseevents";
        final String TAG = "CouchbaseEvents";
        Database database = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Begin Couchbase Events App");
        Manager manager = null;
        try {
            manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
            database = manager.getDatabase(DB_NAME);
        } catch (Exception e) {
            Log.e(TAG, "Error getting database", e);
            return;
        }
        Document createdDocument = createDocument(database);
        Log.d(TAG, "createdDocument=" + String.valueOf(createdDocument.getProperties()));
        // retrieve the document from the database
        Document retrievedDocument = database.getDocument(createdDocument.getId());
    // display the retrieved document
        Log.d(TAG, "retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
        updateDoc(database, createdDocument.getId());
        Document updatedDocument = database.getDocument(createdDocument.getId());
        Log.d(TAG, "updatedDocument=" + String.valueOf(updatedDocument.getProperties()));
        try {
            startReplications();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "End Couchbase Events App");
        }
        private void updateDoc(Database database, String documentId) {
        Document document = database.getDocument(documentId);
        try {
            // Update the document with more data
            Map<String, Object> updatedProperties = new HashMap<String, Object>();
            updatedProperties.putAll(document.getProperties());
            updatedProperties.put("eventDescription", "Everyone is invited!");
            updatedProperties.put("address", "123 Elm St.");
            // Save to the Couchbase local Couchbase Lite DB
            document.putProperties(updatedProperties);
        } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Error putting", e);
        }
        }
        private Document createDocument(Database database) {
        // Create a new document and add data
        Document document = database.createDocument();
        String documentId = document.getId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "Big Party");
        map.put("location", "My House");
        try {
            // Save the properties to the document
            document.putProperties(map);
        } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Error putting", e);
        }
        Document document1 = database.createDocument();
        documentId = document1.getId();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("name", "Yagyank");
        map1.put("location", "GGn");
        try {
            // Save the properties to the document
            document1.putProperties(map1);
        } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Error putting", e);
        }
        return document1;
        }
        private URL createSyncURL(boolean isEncrypted){
        URL syncURL = null;
        String host = "https://127.0.0.1";
        String port = "8091";
        String dbName = "sync_gateway";
        try {
            syncURL = new URL(host + ":" + port + "/" + dbName);
        } catch (MalformedURLException me) {
            me.printStackTrace();
        }
        return syncURL;
        }
        private void startReplications() throws CouchbaseLiteException {
        Replication pull = database.createPullReplication(this.createSyncURL(false));
        Replication push = database.createPushReplication(this.createSyncURL(false));
        pull.setContinuous(true);
        push.setContinuous(true);
        pull.start();
        push.start();
          /*  if(!push.isRunning()){
            Log.d(TAG, "MyBad");
        }*/
        /*if(!push.isRunning()) {
            Log.d(TAG, "Replication is not running due to " +push.getLastError().getMessage());
            Log.d(TAG, "Replication is not running due to " +push.getLastError().getCause());
            Log.d(TAG, "Replication is not running due to " +push.getLastError().getStackTrace());
            Log.d(TAG, "Replication is not running due to " +push.getLastError().toString());
        }*/
        }

    }


I used this api (http://developer.couchbase.com/documentation/mobile/1.2/develop/references/couchbase-lite/rest-api/document/put---db---doc-/index.html) and it works fine

Jens Alfke

unread,
May 3, 2016, 2:57:26 PM5/3/16
to mobile-c...@googlegroups.com

On May 3, 2016, at 6:07 AM, Yagyank Chadha <yag...@gmail.com> wrote:

I am trying to use CouchBase Lite for my mobile app. I have seen in tutorial of couchbase but it shows how to add sync URL for walrus server not couchbase server. How can I set up sync URL for couchbase server.

Couchbase Lite connects to Sync Gateway, not to the database (whether it’s Walrus or Couchbase.) So the answer is that it doesn’t matter to CBL what kind of database back-end you’re using.

In your example the Sync Gateway’s interface is port 4986, so that’s the port you’d use in the sync URL in CBL, along with the hostname of the server running Sync Gateway and the name of the database (not bucket).

—Jens
Reply all
Reply to author
Forward
0 new messages