如何通过 Apache 的 HttpClient 在 Couchdb 中设置管理员(给出了一个 curl 示例)

发布于 2024-10-02 18:40:32 字数 372 浏览 12 评论 0原文

因此,我正在开发一个 CouchDB Gui 工具箱,以便更轻松地维护 Android 上的 CouchDB 设置,因为 Futon 在小型移动设备上非常不舒服。

我想坚持使用“org.apache.http.client.*”包,在我想设置管理员之前,它运行得很好。

使用命令行工具“curl”,它的工作方式就像一个魅力:

curl -X PUT http://127.0.0.1:5984/_config/admins/username -d '"password"'

但我保留将其转换为“org.apache.http.client.methods.HttpPut()”方法时遇到大问题。

任何帮助表示赞赏。

So I am working on a CouchDB Gui Toolbox for easier maintaining an setting up CouchDB on Android, as Futon is quite uncomfortable on a small mobile device.

I wanted to stick to the "org.apache.http.client.*" packages for this which was working out quite well until I wanted to setup administrators..

With the commandline tool "curl" it works like a charm:

curl -X PUT http://127.0.0.1:5984/_config/admins/username -d '"password"'

But I keep on having big problems translating that to a "org.apache.http.client.methods.HttpPut()" method.

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻想少年梦 2024-10-09 18:40:32
DefaultHttpClient client = new DefaultHttpClient();

HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");
put.setEntity(new StringEntity("\"password\""));
client.execute(put);
DefaultHttpClient client = new DefaultHttpClient();

HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");
put.setEntity(new StringEntity("\"password\""));
client.execute(put);
草莓酥 2024-10-09 18:40:32

是的,抱歉。为了完成答案,以下是实际处理我收到的请求响应的方法:

    DefaultHttpClient client = new DefaultHttpClient();
    JSONObject json = null;

    HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");

    try {
        StringEntity strEntity = new StringEntity("\"" + password + "\"");
        put.setEntity(strEntity);
        response = client.execute(put);
        HttpEntity responseEntity = response.getEntity();
    //Or do something with the entity of the response  
    // if (response.getStatusLine().getStatusCode() == 200) {
    //      return something;
    //  }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();  
    } catch (IOException e) {
        e.printStackTrace();  
    }

Yes, sorry. Just to complete the answer, here's how to actually deal with the response I get for the request:

    DefaultHttpClient client = new DefaultHttpClient();
    JSONObject json = null;

    HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");

    try {
        StringEntity strEntity = new StringEntity("\"" + password + "\"");
        put.setEntity(strEntity);
        response = client.execute(put);
        HttpEntity responseEntity = response.getEntity();
    //Or do something with the entity of the response  
    // if (response.getStatusLine().getStatusCode() == 200) {
    //      return something;
    //  }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();  
    } catch (IOException e) {
        e.printStackTrace();  
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文