使用 JoddHttp put request to send JSON with the .body() corrupts Chinese text

Using JoddHttp put request to send JSON with the .body() corrupts Chinese text

我的方法如下:

public String submitMaterials(String url,JSONObject params) {
    return HttpRequest
        .create("put", url)
        .mediaType(MediaType.APPLICATION_JSON_UTF8_VALUE)
        .body(params.toJSONString())
        .send()
        .bodyText();
}

我导入的参数:

请帮帮我,谢谢!

我这样做,是对的:

public String submitMaterials(String url,JSONObject params) {
     return HttpRequest
           .create("put", url)
           .mediaType("application/json;charset=UTF-8")
           .bodyText(params.toJSONString(),“UTF-8”)
           .contentType("application/json;charset=UTF-8")
           .send();
 }

不要使用mediaType,它只是ContentType的一部分,没有设置编码。所以只需使用 contentType() 代替:

return HttpRequest
        .create("put", url)
        .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
        .body(params.toJSONString())
        .send()
        .bodyText();
}

请注意,您可以使用 contentType 的 2 参数版本来发送媒体类型和内容:

        .contentType("application/json", "UTF8")

mediaType 方法的编写版本将被删除,只是为了不混淆人们。也请参阅 javadoc