java - 在 Android 中将 JSONObject 上传到 Iris CouchDb

标签 java android json couchdb iriscouch

我目前正在开发一个应用程序,但在将 JSONObject 上传到我的 Iris CouchDb 时遇到问题。但我无法让它工作。

这是我现在正在使用的代码:

private class MyHttpPost extends AsyncTask<String, Void, Boolean> {


    @Override
    protected Boolean doInBackground(String... arg0)
    {

        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
                HttpVersion.HTTP_1_1);
        HttpClient httpClient = new DefaultHttpClient(params);
        HttpPost httpPost = new HttpPost("https://dbname.iriscouch.com/dbname");

        try {
            // Add your data
            JSONObject jsonDoc = new JSONObject();
            try {
                jsonDoc.put("name", "test");
                jsonDoc.put("autor", "test author");
                jsonDoc.put("rundgangnr", "1");
                jsonDoc.put("latitude", 58.0);
                jsonDoc.put("longitude", 7.88);

            } catch (JSONException e) {
                e.printStackTrace();
            } // end try
            String body = jsonDoc.toString();
            StringEntity entity = new StringEntity(body, "utf-8");
            httpPost.setEntity(entity);

            // Execute HTTP Post Request
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            InputStream result = httpEntity.getContent();

        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return true;
    }

}

在 onCreate 中,我这样做是为了执行该函数:

new MyHttpPost().execute();

当我运行应用程序时,没有错误,但没有上传任何内容。所以数据库没有任何变化。

我是否使用了错误的 URL 将其上传到 Iris,或者代码有问题?我是 Android 开发新手,非常感谢您的帮助,因为我已经为此苦苦挣扎了好几天。

最佳答案

也许您需要使用 https://accountname.iriscouch.com/dbname 而不是 https://dbname.iriscouch.com/dbname

此外,如果请求抛出 IOException,您的应用程序将默默地吞下该错误,因此您可能看不到它。

关于java - 在 Android 中将 JSONObject 上传到 Iris CouchDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831979/

相关文章:

android - 如何在android中的imageviews之间切换图像?

Android 源代码生成器 : Error: Can't find bundle for base name messages. AndroidJpsBundle

java - 循环顺序: how to choose the outer loop

java - 权限拒绝 : MediaDocumentsProvider

java - 在Java中将十六进制转换为十进制

javascript - Highstock - 如何在折线图或面积图工具提示中显示开盘价、最高价、最低价、收盘价

json - ContextualDeserializer,用于使用 Jackson 将 JSON 映射到不同类型的映射

java - 根据另一个 JComboBox 的选择更改 JComboBox 的元素

没有添加权限的 LVL 的 Android 许可 DeviceId

java - 我只想解析 json,其中部分是 2 两个。但响应所有 json 文件解析