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

210 Views Asked by At

My method is as follows:

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

The parameters I import: enter image description here

enter image description here

Please help me,thanks!

2

There are 2 best solutions below

1
igr On BEST ANSWER

Don't use the mediaType, it is just a part of ContentType, that does not set the encoding. So just use the contentType() instead:

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

Note that you can use 2-argument version of contentType that sends media type and the content:

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

Write version of mediaType method will be removed just not to confuse people. See the javadoc too.

1
D.D On

i do this ,it's right:

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();
 }