cloudant / java-cloudant

A Java client for Cloudant
Apache License 2.0
79 stars 68 forks source link

error in active sync when trying to get DB #328

Closed zeev-mindali closed 7 years ago

zeev-mindali commented 7 years ago

Please include the following information in your ticket.

new AsyncTask<Void, Void ,String>() {

        @Override
        protected String doInBackground(Void... voids) {
            CloudantClient client = ClientBuilder.account(DB_USER_NAME)
                    .username(TEXT_API_KEY)
                    .password(TEXT_API_SECRET)
                    .build();

            List<String> databases = client.getAllDbs();
            return databases.toString();

            }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
        }
    }.execute();

when trying to use the software , i getting error regarding doInBackground error. the error starting in client.getAllDbs method.

please advice

ricellis commented 7 years ago

It looks like you are trying to getAllDbs() using an API key. Cloudant API keys cannot be used for account level calls e.g. listing or creating databases. If you want to list the databases use account level credentials or when using the API key only make calls against the database the key is associated with.

zeev-mindali commented 7 years ago

i wrote the following code, the API keys are correct:

new AsyncTask<Void, Void ,String>() {

        @Override
        protected String doInBackground(Void... voids) {
            CloudantClient client = ClientBuilder.account(DB_USER_NAME)
                    .username(TEXT_API_KEY)
                    .password(TEXT_API_SECRET)
                    .build();

            //return databases.toString();
            Database db = client.database("example_db", false);
            // A Java type that can be serialized to JSON
            class ExampleDocument {
                private String _id = "example_id";
                private String _rev = null;
                private boolean isExample;

                public ExampleDocument(boolean isExample) {
                    this.isExample = isExample;
                }

                public String toString() {
                    return "{ id: " + _id + ",\nrev: " + _rev + ",\nisExample: " + isExample + "\n}";
                }

            }
            db.save(new ExampleDocument(true));
            return "working";
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
        }
    }.execute();

still getting error, this time on async :( FATAL EXCEPTION: AsyncTask #1 Process: com.safecheck.android.repo, PID: 1958 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.IllegalStateException: Not a JSON Object: null at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90) at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:404) at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:392) at com.cloudant.client.org.lightcouch.CouchDatabaseBase.save(CouchDatabaseBase.java:196) at com.cloudant.client.api.Database.save(Database.java:710) at com.safecheck.android.repo.RepoMain$1.doInBackground(RepoMain.java:85) at com.safecheck.android.repo.RepoMain$1.doInBackground(RepoMain.java:59) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:818) 

ricellis commented 7 years ago

Your ExampleDocument class needs to be a static inner class or a standalone class GSON doesn't automatically serialize non-static inner classes.

zeev-mindali commented 7 years ago

thanks!!! working finally like a charm

ricellis commented 7 years ago

Great