【问题标题】:Special characters disappear in POST request from a android phone特殊字符在来自 Android 手机的 POST 请求中消失
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多