2016-05-29 2 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()ではありません。これは、ことになっている

CU

(私は手動でこれを入力した)が、私はコードから得たもの:

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"の上になければなりません。しかし、単語全体をコピーしてエディタに貼り付けると、まったく同じように表示されます。これは、私がここでそれを貼り付けるときのようになります。

CU

、これは私がフックをコピーする場合に発生するものであるだけ

̉

彼ら同じように見えますが、EditTextのものは今後の作業には使用できません。

インターネットから単語をコピーして検索フィールドに貼り付けると機能します。だから問題を引き起こしたのは私の入力方法です。それらをユニコード(\ xxxxのもの)に変換することを検索しようとしましたが運がありません。

誰でも入力を「標準形式」に変換する方法はありますか?あなたの時間をありがとう!

答えて

1

正しい形式のテキストをAPIで送信することに問題がある場合は、HttpClientでUTF(使用しているUTFフォントと仮定)を設定してください。ログに表示される内容を心配しないでください。カスタムフォントは、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 =新しいHttpPost(params [0]); HttpResponseレスポンス= httpclient.execute(httppost); jsonResult = inputStreamToString( response.getEntity()。getContent())。toString(); jsonResult = jsonResult。substring(6); ' –

+0

コードを少し修正しました。あなたが現在POST要求を実行している方法では、ペイロードがないようです。 StringEntityはあなたのPOSTペイロード/ params(NameValuePairまたはJSONオブジェクト)を取り、それをUTFにエンコードします。 – fluffyBatman

+0

ああ、申し訳ありませんが、言及を忘れてしまった。 params [0]はGET paramsとの完全なリンクです。 –

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 + "&"; 
     } 
    } 
関連する問題