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"));
精彩评论