1

尝试在我的 IrisCouch 数据库中创建新文档时,我总是收到以下错误:{"error":"bad_request","re​​ason":"invalid_json"}

这是我的功能的相关部分:

try {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("https://username.iriscouch.com:6984/mydb");
    StringEntity entity = new StringEntity(body);

    httpPost.setEntity(entity);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");

    HttpResponse httpResponse = httpClient.execute(httpPost);
    System.out.println(httpResponse.toString()) ;
    HttpEntity httpEntity = httpResponse.getEntity();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(httpEntity.getContent()));

    String line = "";
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }

    System.out.println("body: " + body);
} catch (Exception e) {
    e.printStackTrace();
}

这是我的 System.out 的“body”:

{"bild1":"","bild2":"","bild3":"","bild4":"","bild5":"","bild6":"","bild7":"","bild8":"","bild9":"","bild10":"","name":"","plz":0,"ort":"","strasse":"","n_koordinate":0,"e_koordinate":0,"beschreibung":"","groesse":"< 50m²","sitzplaetze":"< 50","grillstellen":"1","reservierung":false,"res_name":"","res_tele":"","res_weiteres":"","wc":true,"behindi":false,"dach":true,"kinderfreundl":false,"auto":false,"kinderwagen":false,"einkauf":false,"datum":"2015-07-01-14:12:01:856","bewertung":0,"anz_bewertung":0}

JSON 是有效的。使用 jsonlint.com JSON Validator 对其进行了测试。

我能做什么?提前致谢!

4

1 回答 1

0

也许 unicode 上标 2 (²) 正在困扰它?不确定这个 HttpPost 是哪个库,但看起来很像 HttpWebRequest,所以尝试这样设置你的标题:

httpPost.setHeader("Content-type", "application/json charset=utf-8");

也可能是 HttpPost 类没有正确编码字符串。(我没有在我的 Visual Studio 中粘贴这个,所以只是在黑暗中拍摄)

于 2015-07-04T00:44:22.723 回答