2016-05-29 38 views
0

我有一個EDITTEXT在搜索字符串填寫:的Android EditText上沒有返回正確的UTF-8文本

<?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()。它應該是(我輸入手動此一)

,但我從代碼有:

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

是像下面的圖片:

enter image description here

我認爲他們是不同的編碼。掛鉤必須在「u」之上。但是,如果我複製整個單詞並粘貼到任何編輯器,它顯示完全相同。這是個什麼樣子,當我在這裏貼吧,如:

,這是,如果我複製鉤什麼只發生

̉

他們看起來相同,但EditText不能用於進一步的作品。

如果我從互聯網複製單詞並粘貼到搜索字段,它的工作原理!所以這是我的輸入法引起的問題。試圖尋找轉換他們到unicode(\ xxxx),但沒有運氣。

任何人有任何想法如何將輸入轉換爲「正常格式」?感謝您的時間!

回答

1

如果您在通過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 
+0

這是我的代碼爲我的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。子串(6);' –

+0

我修改了一下代碼。您當前執行POST請求的方式似乎沒有任何有效負載。 StringEntity將您的POST負載/參數(NameValuePair或JSON對象)編碼爲UTF。 – fluffyBatman

+0

哦對不起,忘了提。 params [0]是GET參數的整個鏈接。 –

0

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

0

這是我經過幾個小時的搜索後才找到的唯一解決方案。

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 + "&"; 
     } 
    }