如何使用Android App

时间:2016-02-09 12:09:25

标签: android ibm-cloud cloudant nosql

我已通过Android App在Cloudant数据库中成功创建了文档。但我找不到Cloudant数据库中的特定文档。我试过的就像下面......

TabTwo_Fragment.java

package com.example.android02.insightapp11;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudant.sync.datastore.BasicDocumentRevision;
import com.cloudant.sync.datastore.Datastore;
import com.cloudant.sync.datastore.DatastoreManager;
import com.cloudant.sync.datastore.DatastoreNotCreatedException;
import com.cloudant.sync.datastore.DocumentBodyFactory;
import com.cloudant.sync.datastore.DocumentException;
import com.cloudant.sync.datastore.DocumentNotFoundException;
import com.cloudant.sync.datastore.DocumentRevision;
import com.cloudant.sync.datastore.MutableDocumentRevision;
import com.cloudant.sync.replication.Replicator;
import com.cloudant.sync.replication.ReplicatorBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 */
public class TabTwo_Fragment extends Fragment implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListener {

    private static final String DATASTORE_MANGER_DIR = "data";
    private static final String TASKS_DATASTORE_NAME = "tasks";

    Datastore DataStore;
    private Replicator PushReplicator, PullReplicator;
    DocumentRevision revision;

    static final String CLOUDANT_USER = "user_default";
    static final String CLOUDANT_DB = "database_name";
    static final String CLOUDANT_API_KEY = "api_key";
    static final String CLOUDANT_API_SECRET = "api_key_pwd";

    long TWITTER_ID = MainActivity.twitterID;
    String TWITTER_NAME = MainActivity.userName;

    Button btn_submit;
    Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8;
    TextView ans_total;

    String user = "old";
    HashMap<String, String> qa_hashmap;

    public TabTwo_Fragment() {
        // Required empty public constructor
    }

    public static TabTwo_Fragment newInstance() {
        return new TabTwo_Fragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tabtwo_fragment, container, false);
        try {
            init(rootView);
        } catch (DocumentNotFoundException e) {
        } catch (DocumentException e) {
        } catch (URISyntaxException e) {
        }
        return rootView;
    }

    void init(View rootView) throws DocumentException, URISyntaxException {
        Log.e("init: ", "in init");
        find_view_by_id(rootView);
        registerClickEvents();
        defaultConfig();
        checkForRegisteredUser();
    }

    void find_view_by_id(View rootView) {
        Log.e("find_view_by_id: ", "in find_view_by_id");
        btn_submit = (Button) rootView.findViewById(R.id.btn_submit);
        sp1 = (Spinner) rootView.findViewById(R.id.sp1);
        sp2 = (Spinner) rootView.findViewById(R.id.sp2);
        sp3 = (Spinner) rootView.findViewById(R.id.sp3);
        sp4 = (Spinner) rootView.findViewById(R.id.sp4);
        sp5 = (Spinner) rootView.findViewById(R.id.sp5);
        sp6 = (Spinner) rootView.findViewById(R.id.sp6);
        sp7 = (Spinner) rootView.findViewById(R.id.sp7);
        sp8 = (Spinner) rootView.findViewById(R.id.sp8);
        ans_total = (TextView) rootView.findViewById(R.id.ans_total);
    }

    void registerClickEvents() {
        Log.e("registerClickEvents: ", "in registerClickEvents");
        btn_submit.setOnClickListener(this);
    }

    void defaultConfig() {
        Log.e("defaultConfig: ", "in defaultConfig");
        PreferenceManager.setDefaultValues(getContext(), R.xml.preferences, false);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        sharedPref.registerOnSharedPreferenceChangeListener(this);

        File path = getContext().getDir(DATASTORE_MANGER_DIR, Context.MODE_PRIVATE);
        DatastoreManager manager = new DatastoreManager(path.getAbsolutePath());

        try {
            DataStore = manager.openDatastore(TASKS_DATASTORE_NAME);
            setUriPlusReplicators();
        } catch (DatastoreNotCreatedException e) {
            Toast.makeText(getContext(), "Unable to open Datastore!", Toast.LENGTH_SHORT).show();
        } catch (URISyntaxException e) {
            Toast.makeText(getContext(), "URI Exception!!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        try {
            setUriPlusReplicators();
        } catch (URISyntaxException e) {
        }
    }

    void setUriPlusReplicators() throws URISyntaxException {
        Log.e("setUriPlusReplicators: ", "in setUriPlusReplicators");
        URI uri = this.createServerUri();
        Log.e("URI: ", "" + uri);
        PushReplicator = ReplicatorBuilder.push().from(DataStore).to(uri).build();
        PushReplicator.getEventBus().register(this);
        PullReplicator = ReplicatorBuilder.pull().to(DataStore).from(uri).build();
        PullReplicator.getEventBus().register(this);
    }

    private URI createServerUri() throws URISyntaxException {
        Log.e("createServerUri: ", "in createServerUri");
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        String username = sharedPref.getString(CLOUDANT_USER, "");
        String dbName = sharedPref.getString(CLOUDANT_DB, "");
        String apiKey = sharedPref.getString(CLOUDANT_API_KEY, "");
        String apiSecret = sharedPref.getString(CLOUDANT_API_SECRET, "");
        String host = username + ".cloudant.com";
        return new URI("https", apiKey + ":" + apiSecret, host, 443, "/" + dbName, null, null);
    }

    private URI createServerUri2(long twitterId) throws URISyntaxException {
        Log.e("createServerUri2: ", "in createServerUri2");
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        String username = sharedPref.getString(CLOUDANT_USER, "");
        String dbName = sharedPref.getString(CLOUDANT_DB, "");
        String apiKey = sharedPref.getString(CLOUDANT_API_KEY, "");
        String apiSecret = sharedPref.getString(CLOUDANT_API_SECRET, "");
        String host = username + ".cloudant.com";
        return new URI("https", apiKey + ":" + apiSecret, host, 443, "/" + dbName + "/_all_docs?", null, null);
    }

    void checkForRegisteredUser() throws DocumentNotFoundException, DocumentException, URISyntaxException {
        BasicDocumentRevision retrieved = DataStore.getDocument(TWITTER_ID + "");
        if (user.equals("new")) {
            setQuizData(retrieved.getBody().toString());
        } else {
            newUser();
        }
    }

    void setQuizData(String DataRetrieved) {
        Log.e("setQuizData: ", "In setQuizData");
        user = "old";
        Log.e("DataRetrieved: ", "In " + DataRetrieved);
        String[] ANSWERS = getResources().getStringArray(R.array.ans);
        try {
            JSONObject jsonObject = new JSONObject(DataRetrieved);
            JSONObject jsonObject1 = jsonObject.getJSONObject("questions");
            String qs1 = jsonObject1.getString("q1");
            String qs2 = jsonObject1.getString("q2");
            String qs3 = jsonObject1.getString("q3");
            String qs4 = jsonObject1.getString("q4");
            String qs5 = jsonObject1.getString("q5");
            String qs6 = jsonObject1.getString("q6");
            String qs7 = jsonObject1.getString("q7");
            String qs8 = jsonObject1.getString("q8");

            sp1.setSelection(Arrays.asList(ANSWERS).indexOf(qs1));
            sp2.setSelection(Arrays.asList(ANSWERS).indexOf(qs2));
            sp3.setSelection(Arrays.asList(ANSWERS).indexOf(qs3));
            sp4.setSelection(Arrays.asList(ANSWERS).indexOf(qs4));
            sp5.setSelection(Arrays.asList(ANSWERS).indexOf(qs5));
            sp6.setSelection(Arrays.asList(ANSWERS).indexOf(qs6));
            sp7.setSelection(Arrays.asList(ANSWERS).indexOf(qs7));
            sp8.setSelection(Arrays.asList(ANSWERS).indexOf(qs8));
        } catch (JSONException e) {
        }
    }

    void newUser() {
        Log.e("newUser: ", "In newUser");
        user = "new";
    }

    void createDocument() throws DocumentException {
        Log.e("createDocument: ", "In createDocument");
        MutableDocumentRevision rev = new MutableDocumentRevision();
        rev.docId = TWITTER_ID + "";
        gatherQAData();

        /*HashMap<String, Object> map_main = new HashMap<String, Object>();*/
        HashMap<String, Object> map = new HashMap<String, Object>();
        ArrayList<HashMap<String, Object>> arrayTeachers = new ArrayList<>();

        map.put("twitter_id", TWITTER_ID + "");
        map.put("twitter_name", TWITTER_NAME);
        map.put("questions", qa_hashmap);
        /*arrayTeachers.add(map);*/
        /*map_main.put("user", arrayTeachers);*/

        rev.body = DocumentBodyFactory.create(map);
        revision = DataStore.createDocumentFromRevision(rev);
        PushReplicator.start();
    }

    void updateDocument() {
        Log.e("updateDocument: ", "In updateDocument");
    }

    void gatherQAData() {
        Log.e("gatherQAData: ", "In gatherQAData");
        qa_hashmap = new HashMap<String, String>();
        String a1, a2, a3, a4, a5, a6, a7, a8;
        a1 = sp1.getSelectedItem().toString();
        a2 = sp2.getSelectedItem().toString();
        a3 = sp3.getSelectedItem().toString();
        a4 = sp4.getSelectedItem().toString();
        a5 = sp5.getSelectedItem().toString();
        a6 = sp6.getSelectedItem().toString();
        a7 = sp7.getSelectedItem().toString();
        a8 = sp8.getSelectedItem().toString();
        qa_hashmap.put("q1", a1);
        qa_hashmap.put("q2", a2);
        qa_hashmap.put("q3", a3);
        qa_hashmap.put("q4", a4);
        qa_hashmap.put("q5", a5);
        qa_hashmap.put("q6", a6);
        qa_hashmap.put("q7", a7);
        qa_hashmap.put("q8", a8);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                if (user.equals("new")) {
                    try {
                        createDocument();
                    } catch (DocumentException e) {
                    }
                } else {
                    updateDocument();
                }
                break;
            default:
        }
    }
}

Doc Stored in Cloudant DB

我可以获取存储在DataStore中的文档。但我无法将文档存储在Cloudant数据库中。

仅供参考:我在checkForRegisteredUser方法中找到文件。

2 个答案:

答案 0 :(得分:0)

是的,我已经做到了。下面是我的TabTwo_Fragment.java {/ 1}}方法的代码

checkForRegisteredUser

以下是官方文档LINK

此处 DOC_ID 是您要检查的文档的ID。希望这会对某人有所帮助。

答案 1 :(得分:0)

Cloudant中发生的情况是默认情况下已经有两个字段由cloudant给予每个文档,即_id和_rev,其中_id充当索引的主键。因此,在_id字段上将由cloudant创建默认索引。如果您的文档中已经包含这些字段而不是cloudant,则不会插入额外的_id和_rev。 现在,您可以在文档中包含此_id字段,并根据自己设置或让cloudant设置它。但是,当您没有设置它时,您将无法在主索引上查询Cloudant,因为您不知道文档的_id。 因此,要根据您存储的某些其他字段(如twitter_id)进行搜索或查找,您必须再创建一个索引,而不是根据该搜索/查找。 我们可以使用cloudant api

提供的函数在java中创建索引
public void createIndex(java.lang.String indexName,
               java.lang.String designDocName,
               java.lang.String indexType,
               IndexField[] fields)

或如果您在函数中分配_id而不是简单地调用函数

db.contains(doc_id);

由于您不需要创建任何索引并使用默认索引,因此更容易。

EDIT-1 所以db这里是你已经获得的数据库实例。

Database db=client.database(db_name,create_flag);

其中 create_flag 布尔,如果数据库应该创建(如果不存在),则true表示是创建false表示如果不存在则不创建。 和客户端是CloudantClient对象。 但是,如果您知道数据库不在那里,您也可以调用createDB。您可以按照以下方式执行此操作

Database db=client.createDB("DBNAME");

如何获取CloudantClient取决于您使用的cloudant api的版本。因为ex 1.0.1与最新的不同。 在版本1.0.1中,您可以按照以下方式执行此操作

CloudantClient client=new CloudantClient(URL_OF_YOUR_CLOUDANT,USERNAME,PASSWORD);

但在最新版本中,他们使用

CloudantClient client=ClientBuilder.url(new URL("your url")).username("your username string").password("your password").build();

所以我不知道为什么你找不到createIndex。我想你无法找到Cloudant-api javadocs。只是google for Cloudant javadocs或者检查这个github GitHub link