How to connect Clusterpoint database to an android appliaction

177 views Asked by At

I am new to NoSQL database and cloud. I am trying to develop a simple application in android using Clusterpoint (DBAAS). I tried and searched so many possibilities, but it is not quite working.

(new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
      String result = "";
            try {

                String requestString = "https://username:password@api-eu" +
                        ".clusterpoint.com/908/users/";

                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse httpResponse = null;
                HttpPost httpPost = new HttpPost(requestString);

                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity httpEntity = response.getEntity();
                result = EntityUtils.toString(httpEntity);
            } catch (IOException e) {
                result = "Error";
                e.printStackTrace();
            } finally {
                Log.v("ClusterResponse", result);
                return result;
            }

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.v("ClusterResponse", s);
        }
    }).execute();

In my code i replaced username and password with original values.

1

There are 1 answers

0
Stenal P Jolly On BEST ANSWER

I got the connection. I am posting my code so the next person can get it right in a much faster way

My mistakes - Needed GET instead of POST - Authorization should be of base64 with NOWRAP , so it won't add "CR" line at the end.

(new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
      String result = "";
            try {

                String requestString = "https://api-eu.clusterpoint.com/908/users/";

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(requestString);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();

                httpget.addHeader("Authorization", "Basic "+Base64.encodeToString
                        ("username:password".getBytes(),Base64.NO_WRAP));

                result = httpClient.execute(httpget, responseHandler);
            } catch (IOException e) {
                result = e.toString();
                e.printStackTrace();
            } finally {
                Log.v("ClusterResponse", "Done");
                return result;
            }

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();
            Log.v("ClusterResponse", s);
        }
    }).execute();