我正在创建一个将数据存储在CouchDB中的Android应用程序,我需要从Android应用程序创建数据库。我需要使用Java方法执行命令“curl-X PUT http://user:passwd@127.0.0.1:5984/myDataBase”。
我已经实现了以下函数:
但是没有结果。我认为问题在于用户名和密码,因为在“管理员模式”下,通过以下方式调用该函数可以正常工作:
我已经实现了以下函数:
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:passwd@127.0.0.1/","myDataBase");
但是没有结果。我认为问题在于用户名和密码,因为在“管理员模式”下,通过以下方式调用该函数可以正常工作:
createDatabase("http://127.0.0.1/","myDataBase");