I´m creating an android application that stores data in CouchDB, and I need to create a database from the android application. I need to execute the command "curl-X PUT http://user:passwd@127.0.0.1:5984/myDataBase" with java methods.
I have implemented the following functions:
public static boolean createDatabase(String hostUrl, String databaseName) {
try {
HttpPut httpPutRequest = new HttpPut(hostUrl + databaseName);
JSONObject jsonResult = sendCouchRequest(httpPutRequest);
return jsonResult.getBoolean("ok");
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static JSONObject sendCouchRequest(HttpUriRequest request) {
try {
HttpResponse httpResponse = (HttpResponse) new DefaultHttpClient().execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String resultString = convertStreamToString(instream);
instream.close();
JSONObject jsonResult = new JSONObject(resultString);
return jsonResult;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I call the function by:
createDatabase("http://user:passwd@127.0.0.1/","myDataBase");
but there is no result. I think the problem is in user:passwd because in "admin party" mode the funcion works fine calling by:
createDatabase("http://127.0.0.1/","myDataBase");