Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters disappear in POST request from a android phone

I have build an android app that let's you post your name on a website though i form that sends a http POST request to the website. The problem is that 90% av my customers are Swedish and the POST request seems to chop of everything after a special character in a string, including the special character itself.

So the Swedish surname "Börjesson", becomes "B".

my POST request code:

public static String execRequest(String url, Map<String, String> params)
{
    try {
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        HttpPost httpPost = null;
        HttpGet httpGet = null;
        if(params == null || params.size() == 0) {
            httpGet = new HttpGet(url);
            httpGet.setHeader("Accept-Encoding", "UTF-8");
        }
        else {
            httpPost = new HttpPost(url);
            httpPost.setHeader("Accept-Encoding", "UTF-8");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            for(String key: params.keySet()) {
                nameValuePairs.add(new BasicNameValuePair(key, params.get(key)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
        HttpResponse httpResponse = (HttpResponse)defaultHttpClient.execute(httpPost == null ? httpGet : httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        if(null != httpEntity) {
            InputStream inputStream = httpEntity.getContent();
            Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
            if(contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("UTF-8")) {
                inputStream = new GZIPInputStream(inputStream);
            }
            String responseString = convertStreamToString(inputStream);
            inputStream.close();
                return responseString;
        }
    }
    catch(Throwable t) {
        t.printStackTrace();
    }
    return null;
}

So, any tips of what I'm doing wrong?

Thanks in advance!

like image 500
Mockarutan Avatar asked Sep 09 '11 12:09

Mockarutan


1 Answers

Use httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

like image 120
Yury Avatar answered Nov 15 '22 08:11

Yury