Search code examples
androidcloudant

How to INSERT DATA in CLOUDANT DATABASE from ANDROID APP


I am working on a ANDROID APP which's database is CLOUDANT stored on IBM's cloud. Actually I am working on PERSONALITY INSIGHTS service of IBM. I want to store quiz data to CLOUDANT database of specific user. I have manually added 2(Two) documents to CLOUDANT database. But I can not find the documentation to insert data to CLOUDANT database via ANDROID APP.

Insert Doc to CLOUDANT database

The above link provides documentation for Reading & Inserting Doc to CLOUDANT Database under heading of Insert & Read A Document but not for ANDROID.

Sample Android App GitHub - CLOUDANT

I was referring above link but I think that example is out of syllabus for me.

What I have tried till is like below..

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.android02.cloudant_db_demo_app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'

    compile (group: 'com.cloudant', name: 'cloudant-sync-datastore-android', version:'latest.release')
}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_user">USERNAME</string>
    <string name="default_dbname">DATABASE</string>
    <string name="default_api_key">API KEY</string>
    <string name="default_api_password">API PWD</string>
</resources>

MainActivity.java

package com.example.android02.cloudant_db_demo_app;

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

import com.cloudant.sync.datastore.MutableDocumentRevision;

public class MainActivity extends AppCompatActivity {

    static final String SETTINGS_CLOUDANT_USER = "USERNAME";
    static final String SETTINGS_CLOUDANT_DB = "DATABASE";
    static final String SETTINGS_CLOUDANT_API_KEY = "API KEY";
    static final String SETTINGS_CLOUDANT_API_SECRET = "API PWD";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MutableDocumentRevision rev = new MutableDocumentRevision();
    }
}

I can also read the data through https://$USERNAME.cloudant.com/$DATABASE/_all_docs. in browser.

Getting Data Image

Any help will be very very appreciable. Thanks in advance.


Solution

  • Update: Dated:- 18.Feb.2016

    You can just create document with necessary data like below using volley library.

    String url = "https://cloudantUsername.cloudant.com/" + DB_NAME;
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("_id", "Document Id What-Ever you want");
    map.put("twitter_feeds", "Your Data");
    
    JsonObjectRequest jar1 = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(map), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.toString());
                        REV = jsonObject.getString("rev");
                    } catch (JSONException e) {
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Json Error Res: ", "" + error);
                }
            });
            requestQueue.add(jar1);
    

    Where _id is document id which you can give. It is also optional. But from my end, you should give your specific document id/name. If you do not give _id, it will take some random _id automatically which is unique. If document successfully created, you will get below response.

    { "ok":true, "id":"id given by you", "rev":"1-2902191555" }

    For more information on this refer this OFFICIAL DOC.