Java – Android post request escape characters

Android post request escape characters… here is a solution to the problem.

Android post request escape characters

How to escape the character “ü” in a string.

I have this character in my json data:

{"Name": "Hyüsin"}

When I do HttpPost on a webServer in my android. It gives me a “bad request” error in response.

HttpPost code:

    // uploads the data
public class UploadData extends AsyncTask<String, Integer, Boolean> {

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

try {

HttpPost request = new HttpPost(LogInActivity.SERVICE_URI + url[0]);

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

THIS IS  {"Name": "Hyüsin"}               
                JSONObject jsonTaakkaart = taakkaart.serializeToObj();

StringEntity entity = new StringEntity(jsonTaakkaart .toString());
                request.setEntity(entity);

DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(request);

return true;

} catch (Exception e) {
                e.printStackTrace();
                return false;
            }

}
    }

Solution

Use:

StringEntity entity = new StringEntity(jsonTaakkaart.toString(), "UTF-8");

Specifies the encoding as UTF-8.

Related Problems and Solutions