繁体   English   中英

如何使用Couchbase Lite将Android应用程序数据存储到Couchbase服务器

[英]How to store android app data to couchbase server with couchbase lite

我需要nosql数据库,所以我使用了沙发床lite来存储数据。 我不知道我是否需要sync_gateway,如果需要,那么如何制作sync_gateway_config.json文件来将数据从移动应用程序存储到沙发床服务器?

这是经理

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

这是文件

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文件

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

    enter code here

是的,您需要同步网关才能将Couchbase Lite与Couchbase Server连接。

在您的代码中,我看不到您实际在哪个位置开始复制。 您需要调用,例如, pull.start();

您的同步网关文件看起来很合理。 Sync Gateway发行版附带一些示例配置文件。 如果遇到问题,请看看那些。

还有一点。 您将复制端点指定为127.0.0.1。 在Android上,这是指设备本身。 如果您在仿真器中运行,则需要使用该仿真器的专用地址。 如果要在设备上进行测试,则需要执行adb reverse tcp:4984 tcp:4984以在设备与开发人员计算机之间建立映射(假设Sync Gateway运行于此)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM