How to send unicode characters in an HttpPost on A

2020-06-08 13:25发布

问题:

I'm trying to allow multilingual support in my app which makes an HTTP post to upload new messages. What do I need to do in order to support japanese & other non latin based languages? my code currently looks something like this:

    //note the msg string is a JSON message by the time it gets here...
private String doHttpPost(String url, String msg)
        throws Exception {

    HttpPost post = new HttpPost(url);

    StringEntity stringEntity = new StringEntity(msg);
    post.setEntity(stringEntity);


    return execute(post);
}

回答1:

Try setting encoding on StringEntity:

StringEntity stringEntity = new StringEntity(msg, "UTF-8");