【问题标题】:Android EditText does not return the correct UTF-8 textAndroid EditText 不返回正确的 UTF-8 文本
【发布时间】:2016-09-27 05:21:13
【问题描述】:

我有一个 Edittext 来填写搜索字符串:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp">

  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/search_input_edittext"
    android:gravity="center_horizontal"
    android:layout_gravity="center_horizontal" />

  <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:padding="10dp"
    android:background="@drawable/add_to_cart_btn"
    android:text="@string/search"
    android:id="@+id/search_search_btn"
    android:stateListAnimator="@null"
    android:layout_gravity="center_horizontal" />
 </LinearLayout>

但是我输入的越南语单词不是正确的getText().getString()。应该是(我手动输入的)

củ

但是我从代码中得到了什么:

EditText search_edittext = (EditText) v_iew.findViewById(R.id.search_input_edittext); //v_iew is from inflating for `AlertDialog`
search_edittext.getText().toString();

如下图:

我认为它们的编码方式不同。钩子必须在“u”上方。但如果我复制整个单词并粘贴到任何编辑器,它显示的内容完全相同。这是我粘贴到这里时的样子:

củ

如果我只复制钩子会发生这种情况

̉

它们看起来一样,但来自EditText 的内容不能用于进一步的工作。

如果我从互联网上复制单词并粘贴到搜索字段中,它就可以了!所以是我的输入法导致了这个问题。试图搜索将它们转换为 unicode(带有 \xxxx 的那个)但没有运气。

有人知道如何将输入转换为“正常格式”吗?感谢您的宝贵时间!

【问题讨论】:

  • 要以您要求的格式获取它,您需要使用规范化表单 C

标签: android encoding utf-8 android-edittext


【解决方案1】:

您可以使用: String sNFC = Normalizer.normalize(search_edittext.getText().toString(), Normalizer.Form.NFC);

【讨论】:

    【解决方案2】:

    如果您在通过 API 发送正确格式的文本时遇到问题,只需在 HttpClient 中设置 UTF(假设它是您正在使用的 UTF 字体)支持。不用担心 Log 中显示的内容。自定义字体很容易在 Log 上渲染不正确。

    HttpPost post = new HttpPost(url);
    StringEntity stringEntity = new StringEntity(payload, "UTF-8");
    
    post.setEntity(stringEntity);
    HttpResponse response = httpclient.execute(httppost);
    

    更新:如果只需要对部分 URL 进行编码,则如下使用它,

    String url = URLEncoder.encode(params[0], "utf-8");
    HttpPost post = new HttpPost(url);
    //Rest of the code 
    

    【讨论】:

    • 这是我的 GET 方法的代码。我应该按照您的建议将 UTF-8 放在哪里? HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(params[0]); HttpResponse response = httpclient.execute(httppost); jsonResult = inputStreamToString( response.getEntity().getContent()).toString(); jsonResult = jsonResult.substring(6);
    • 我稍微修改了代码。您当前执行 POST 请求的方式似乎没有任何有效负载。 StringEntity 获取您的 POST 有效负载/参数(NameValuePair 或 JSON 对象)并将其编码为 UTF。
    • 哦,对不起,忘了说。 params[0] 是与 GET 参数的整个链接。
    • 谢谢!你是生命的救星!
    • 很高兴它有效!另一件事,尝试根据您的 API 类型使用 HttpGet 和 HttpPost(例如,您可以为此 API 使用 HttpGet,因为 API 本身是一个普通的 GET API)。否则,您通常可能无法找出 API 调用错误背后的原因。
    【解决方案3】:

    这是我经过数小时搜索后找到的唯一解决方案。

    String urlParameters = "";//sn=C02G8416DRJM&cn=&locale=&caller=&num=12345
        if(parameters != null) {
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
    
                String val;
                if(!TextUtils.isEmpty(entry.getValue()))
                    val = URLEncoder.encode(entry.getValue());
                else
                    val = "";
    
                urlParameters += entry.getKey() + "=" + val + "&";
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 2011-05-25
      • 2016-12-14
      • 2011-06-27
      相关资源
      最近更新 更多