Android EditText不返回正确的UTF-8文本
我有一个Edittext来填充搜索字符串:Android EditText不返回正确的UTF-8文本,android,encoding,utf-8,android-edittext,Android,Encoding,Utf 8,Android Edittext,我有一个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"
<?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>
如下图所示:
我认为它们的编码不同。吊钩必须位于“u”的上方。但是,如果我复制整个单词并粘贴到任何编辑器中,它会显示完全相同的内容。这是我将其粘贴到此处时的外观:
củ
如果我只复制钩子,就会发生这种情况
̉
它们看起来一样,但从EditText
中得到的内容不能用于进一步的工作
如果我从互联网上复制这个词并粘贴到搜索字段中,它就会工作!所以是我的输入法造成了这个问题。尝试搜索将其转换为unicode(带有\xxxx的),但没有成功
有人知道如何将输入转换为“正常格式”吗?谢谢你抽出时间 如果您在通过API发送正确格式的文本时遇到问题,只需在HttpClient中设置UTF(假设您使用的是UTF字体)支持。不要担心日志中显示的内容。自定义字体很容易在日志中呈现不正确
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
如果您在通过API发送正确格式的文本时遇到问题,只需在HttpClient中设置UTF(假设您使用的是UTF字体)支持。不要担心日志中显示的内容。自定义字体很容易在日志中呈现不正确
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
您可以使用:
字符串sNFC=Normalizer.normalize(search_edittext.getText().toString(),Normalizer.Form.NFC) 您可以使用:
字符串sNFC=Normalizer.normalize(search_edittext.getText().toString(),Normalizer.Form.NFC) 这是我在数小时的搜索后找到的唯一解决方案
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 + "&";
}
}
String url参数=”//sn=C02G8416DRJM&cn=&locale=&caller=&num=12345
if(参数!=null){
对于(Map.Entry:parameters.entrySet()){
字符串val;
如果(!TextUtils.isEmpty(entry.getValue()))
val=URLEncoder.encode(entry.getValue());
其他的
val=”“;
urlParameters+=entry.getKey()+“=”+val+“&”;
}
}
这是我在数小时的搜索后找到的唯一解决方案
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 + "&";
}
}
String url参数=”//sn=C02G8416DRJM&cn=&locale=&caller=&num=12345
if(参数!=null){
对于(Map.Entry:parameters.entrySet()){
字符串val;
如果(!TextUtils.isEmpty(entry.getValue()))
val=URLEncoder.encode(entry.getValue());
其他的
val=”“;
urlParameters+=entry.getKey()+“=”+val+“&”;
}
}
这是我的GET方法的代码。我应该按照你的建议把UTF-8放在哪里<代码>HttpClient HttpClient=新的DefaultHttpClient();HttpPost HttpPost=新的HttpPost(参数[0]);HttpResponse response=httpclient.execute(httppost);jsonResult=inputStreamToString(response.getEntity().getContent()).toString();jsonResult=jsonResult.substring(6)代码>我稍微修改了代码。您当前执行POST请求的方式似乎没有任何有效负载。StringEntity接受您的POST有效负载/参数(NameValuePair或JSON对象)并将其编码为UTF。哦,对不起,忘了提一下。参数[0]是获取参数的全部链接。谢谢!你是救命恩人!很高兴它起作用了!另一件事,根据您的API类型尝试使用HttpGet&HttpPost(例如,您可以将HttpGet用于此API,因为API本身是一个普通的GET API)。否则,您常常会在找出API调用错误背后的原因时遇到麻烦。我应该按照你的建议把UTF-8放在哪里<代码>HttpClient HttpClient=新的DefaultHttpClient();HttpPost HttpPost=新的HttpPost(参数[0]);HttpResponse response=httpclient.execute(httppost);jsonResult=inputStreamToString(response.getEntity().getContent()).toString();jsonResult=jsonResult.substring(6)代码>我稍微修改了代码。您当前执行POST请求的方式似乎没有任何有效负载。StringEntity接受您的POST有效负载/参数(NameValuePair或JSON对象)并将其编码为UTF。哦,对不起,忘了提一下。参数[0]是获取参数的全部链接。谢谢!你是救命恩人!很高兴它起作用了!另一件事,根据您的API类型尝试使用HttpGet&HttpPost(例如,您可以将HttpGet用于此API,因为API本身是一个普通的GET API)。否则,您常常会在找出API调用错误背后的原因时遇到麻烦。