我正在创建一个将数据存储在CouchDB中的android应用程序,我需要从android应用程序创建一个数据库。我需要用java方法执行命令“curl-X PUT http://user:[email protected]:5984/myDataBase”。如何在CouchDB中创建一个数据库,指示用户名和密码
我已实现了以下功能:
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;
}
我通过调用该函数:
createDatabase("http://user:[email protected]/","myDataBase");
但目前还没有结果。我认为这个问题是在用户:passwd文件,因为“管理方”模式funcion工作正常调用由:
createDatabase("http://127.0.0.1/","myDataBase");
对android应用程序环境不太熟悉,但只是为了偏执你尝试了终端上的实际curl命令,要100%确定你使用的用户:passwd是正确的? – rossdavidh 2011-04-12 18:32:30