0

I need nosql database so I used couchbase lite to store data. I dont know if I need sync_gateway and if needed then how to make sync_gateway_config.json file to store data from mobile app to couchbase server?

This is manager

try {
    manager = new Manager(new AndroidContext(getApplicationContext()),  
          Manager.DEFAULT_OPTIONS);
    Manager.enableLogging("Sync", Log.VERBOSE);

    } catch (IOException e) {
         e.printStackTrace();
    }

try {
    database = manager.getDatabase("app");
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}

and This is Document



    Map properties = new HashMap();
        properties.put("p1x", p1[0]);
        properties.put("p1y", p1[1]);
        properties.put("p2x", p2[0]);
        properties.put("p2y", p2[1]);
        properties.put("p3x", p3[0]);
        properties.put("p3y", p3[1]);
        properties.put("x", finalX);
        properties.put("y", finalY);
        // Create a new document
        Document document = database.createDocument();
        // Save the document to the database
        try {
            document.putProperties(properties);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        // Log the document ID (generated by the database)
        // and properties
        Log.d("app", String.format("Document ID :: %s", document.getId()));
        Log.d("app", String.format("coordinate x : %s %s , coordinate y : 
    %s %s , coordinate z : %s %s , finalx : %s finaly : %s", 
    document.getProperty(String.valueOf("p1x")), 
    document.getProperty(String.valueOf("p1y")), 
    document.getProperty(String.valueOf("p2x")), 
    document.getProperty(String.valueOf("p2y")), 
    document.getProperty(String.valueOf("p3x")), 
    document.getProperty(String.valueOf("p3y")), 
    document.getProperty(String.valueOf("x")), 
    document.getProperty(String.valueOf("y"))));

        URL url = null;
        try {
            url = new URL("http://127.0.0.1:8091/wifi");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Replication push = database.createPushReplication(url);
        Replication pull = database.createPullReplication(url);
        push.setContinuous(true);
        pull.setContinuous(true);

Sync_gateway_config file


    {
  "databases": {
    "db": {
      "bucket": "wifi",
      "username": "admin",
      "password": "adminadmin",
      "server": "http://localhost:8091",
      "enable_shared_bucket_access": true,
      "import_docs": "continuous"
    }
  }
}

    enter code here

Varis Bhalala
  • 191
  • 1
  • 1
  • 4

1 Answers1

0

Yes, you need Sync Gateway to connect Couchbase Lite with Couchbase Server.

In your code, I don't see where you actually start the either replication. You need to call, for example, pull.start();.

Your Sync Gateway file looks reasonable. Sync Gateway distributions come with several sample configuration files. Take a look at those if you're having trouble.

Another point. You're specifying your replication endpoint as 127.0.0.1. On Android, this refers to the device itself. If you're running in an emulator, you need to use the special purpose address for that emulator. If you're testing on a device, you need to do something like adb reverse tcp:4984 tcp:4984 to set up a mapping between the device and your dev machine (assuming that's where Sync Gateway is running).

Hod
  • 2,236
  • 1
  • 14
  • 22