开发者

Special characters disappear in POST request from a android phone

开发者 https://www.devze.com 2023-04-03 14:54 出处:网络
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 re

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")) {
                input开发者_StackOverflow社区Stream = 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消