特殊字符在Android手机的POST请求中消失

Moc*_*tan 5 post android http-post special-characters

我已经建立了一个Android应用程序,让你在网站上发布你的名字,虽然我发送一个http POST请求到网站.问题是,我的客户中有90%是瑞典语,并且POST请求似乎会在字符串中包含特殊字符之后切断所有内容,包括特殊字符本身.

所以瑞典姓氏"Börjesson"变成了"B".

我的POST请求代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

那么,我做错了什么提示?

提前致谢!

Yur*_*ury 7

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