【问题标题】:Android HttpRequest HttpPost diacriticAndroid HttpRequest HttpPost 变音符号
【发布时间】:2014-04-28 00:26:47
【问题描述】:

我对变音符号有疑问。

当我尝试在我的网络上手动添加用户时,所有内容都完美保存在数据库中(还有 ě、š、č、ř、ž、ý、á、í、é 等字符),但是当我保存字符串时

String diacritic = "ěščřřžžýáíé";

并将它传递给 NameValuePair 参数中的 getJSONFromUrl,它会保存到数据库中,如下所示:

?????

我真的不知道如何让它工作

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        HttpParams arg = new BasicHttpParams();
        HttpProtocolParams.setVersion(arg, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(arg, "utf-8");

        // defaultHttpClient
        HttpClient httpClient = new DefaultHttpClient(arg);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        String jsonText = EntityUtils.toString(httpEntity, HTTP.UTF_8);

        try {
            jObj = new JSONObject(jsonText);            
        } catch (JSONException e) {
            Log.w("JSON Parser", "Error parsing data " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return jObj;
}

像这样保存:

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", "fb_register"));
    params.add(new BasicNameValuePair("diacritic", "ěščřžýáíé"));
    JSONObject json = jsonParser.getJSONFromUrl(URL, params);

也试过

Charset.forName("UTF-8").encode(diacritic);
params.add(new BasicNameValuePair("diacritic", diacritic));

【问题讨论】:

  • 你的数据库编码是 utf-8 吗?
  • 数据库、表、列...全部utf8_bin

标签: android json http-request


【解决方案1】:

解决了!

// defaultHttpClient
    HttpClient httpClient = new DefaultHttpClient(arg);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); //add encoding

【讨论】:

  • 在你的答案旁边打勾,让你的问题得到回答,这样它就不会出现在未解决的问题中
  • "你可以在 2 天内接受你的答案" - 抱歉,马上尝试了
猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 2014-09-13
  • 2021-11-06
相关资源
最近更新 更多