Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

My First TouchDB Replication

mschoch edited this page Jul 18, 2012 · 13 revisions

This guide will help you get started with TouchDB-Android and perform your first replication.

  1. Add the TouchDB Projects to your Eclipse workspace (as described here Building-TouchDB-Android)
  2. Create a new Android Project (targeting Android SDK 2.2 or newer)
  3. Add the Android Library references to TouchDB-Android and TouchDB-Android-Ektorp
  4. Add the following static initializer block to the Application's main activity:
  {
      TDURLStreamHandlerFactory.registerSelfIgnoreError();
  }
  1. Add the following code to the onCreate() method of the Application's main activity:
      // start TouchDB
      TDServer server = null;
      String filesDir = getFilesDir().getAbsolutePath();
      try {
        server = new TDServer(filesDir);
      } catch (IOException e) {
        Log.e(TAG, "Error starting TDServer", e);
      }

      // start TouchDB-Ektorp adapter
      HttpClient httpClient = new TouchDBHttpClient(tdserver);
      CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

      // create a local database
      CouchDbConnector dbConnector = server.createConnector("testdb", true);

      // pull this database to the test replication server
      ReplicationCommand pushCommand = new ReplicationCommand.Builder()
          .source("https://example.iriscouch.com/example_db")
          .target("testdb")
          .continuous(true)
          .build();

      ReplicationStatus status = dbInstance.replicate(pushCommand);