编程语言
首页 > 编程语言> > Java-Android发布请求转义字符

Java-Android发布请求转义字符

作者:互联网

如何在字符串中转义字符“ü”.

我的json数据中有这个字符:

{"Name": "Hyüsin"}

当我在android中将HttpPost发送到webServer时.它给我一个“错误的请求”错误作为响应.

HttpPost代码:

    // 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;
            }

        }
    }

解决方法:

采用:

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

指定编码为UTF-8.

标签:http,character,escaping,java,android
来源: https://codeday.me/bug/20191101/1983636.html