我是NoSQL数据库和云的新手。我正在尝试使用Clusterpoint(DBAAS)在android中开发一个简单的应用程序。我尝试并搜索了很多可能性,但它并不是很有效。
(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();
在我的代码中,我用原始值替换了用户名和密码。
答案 0 :(得分:0)
我得到了连接。我发布了我的代码,以便下一个人可以更快地完成它
我的错误 - 需要GET而不是POST - 使用NOWRAP授权应为base64,因此最后不会添加“CR”行。
(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();