Android save and retrieve object in couchbase lite

In this post, we will walk through how to save an object to couchbase lite database in a document, and how to retrieve the document and recover the object from the document. For demonstration purposes, the Manager, Database objects needed for interacting with couchbase lite databae are created and passed into the save and retireve method, and the document id is hardcoded as 123.

1. Add these to the dependencies in your android app build gradle file. gson is needed to convert object to json string and json string to object.

compile 'com.couchbase.lite:couchbase-lite-android:1.2.0'
compile 'com.google.code.gson:gson:2.6.2'

2. Create an sample java pojo class, Movie.java, we will use this class to create a Movie object.

public class Movie {
    private String title;
    private String overview;
    private int length;

    public Movie(String title, String overview, int length) {
        this.title = title;
        this.overview = overview;
        this.length = length;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getOverview() {
        return overview;
    }

    public void setOverview(String overview) {
        this.overview = overview;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}

3. Add the following in the onCreate method in an Activity class.

try {
    Manager manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
    Database couchDb = manager.getDatabase("moviedb");
    saveMovie(manager, couchDb, "123"); // make 123 the document id
    retrieveMovie(manager, couchDb, "123");
} catch (Exception e) {
    Log.e("error", e);
}

4. Save a movie object in a document in the couchbase lite database.

private void saveMovie(Manager manager, Database couchDb, String docId) {
    Map<String, Object> docContent = new HashMap<String, Object>();
    docContent.put("message", "Hey Couchbase Lite");
    docContent.put("movie", new Movie("Star War", "The force awakens!", 120));
    Log.i("saveMovie", "docContent=" + String.valueOf(docContent));

    // create an empty document, add content and write it to the couchDb
    Document document = new Document(couchDb, docId);
    try {
        document.putProperties(docContent);
        tvLogWindow.setText(tvLogWindow.getText().toString() + "\n\nDocument written to couchDb named " + dbname + " with ID = " + document.getId());
    } catch (CouchbaseLiteException e) {
        Log.i("saveMovie", "Failed to write document to Couchbase database!");
    }
}

5. Retrieve the movie object from the couchbase lite databse. Retrieve the document -> get the object -> Convert the object to json string using Gson -> convert the json string to Movie object using Gson.

private void retrieveMovie(Manager manager, Database couchDb, String docId) {
	Document retrievedDocument = couchDb.getDocument(docId); // Retrieve the document by id
    Object movieObj = retrievedDocument.getProperties().get("movie");
    Gson gson = new Gson();
    String jsonString = gson.toJson(movieObj, Map.class); //Convert the object to json string using Gson
    Movie movie = gson.fromJson(jsonString, Movie.class); //convert the json string to Movie object
    Log.i("getMovieFromDocument", "jsonString>>>" + jsonString);
    Log.i("getMovieFromDocument", "movie>>>" + movie.getTitle());
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search