【发布时间】:2011-09-09 12:30:37
【问题描述】:
我已经构建了一个 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;
}
那么,关于我做错了什么的任何提示?
提前致谢!
【问题讨论】:
-
你试过
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs), "utf-8"); -
我找不到任何具有该参数的函数...
-
该死的我没有 setEntity bat UrlEncodedXXX 构造函数
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8")); -
成功了! :D 非常感谢!将其发布为答案:)
标签: android post special-characters http-post