Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将字符串转换为unicode/utf以传递url?_Android_Url_Unicode_Encode - Fatal编程技术网

Android 如何将字符串转换为unicode/utf以传递url?

Android 如何将字符串转换为unicode/utf以传递url?,android,url,unicode,encode,Android,Url,Unicode,Encode,我的字符串是: TĐT70,丘林,TùngẢ新罕布什尔州ứ次氯酸钠ọ, 越南哈特恩。 在url查询字符串中传递此文本时,如下所示: ĐT70%2C+chau+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Há+Tĩnh%2C+越南。 但我想要这种格式: \U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129

我的字符串是:

TĐT70,丘林,TùngẢ新罕布什尔州ứ次氯酸钠ọ, 越南哈特恩。

在url查询字符串中传递此文本时,如下所示:

ĐT70%2C+chau+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Há+Tĩnh%2C+越南。

但我想要这种格式:

\U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam.

我怎样才能得到这种格式的字符串?请回答。

经过努力,我终于找到了解决办法

 //String that you want to convert
  String mystring = "133 Phùng Hưng, Cửa Đông, Hoàn Kiếm, Hà Nội, Vietnam";

    //unicodeString is the expected output in unicode 
    String unicodeString = getUnicodeString(escapeUnicodeText(mystring));

   //i want to make small u into Capital from unicode String
  String resultUnicode = unicodeString.replace("\\u", "\\U");

    try {
        String text = URLEncoder.encode(mystring, "UTF-8");
        Log.v("currentDateTimeString", "text String:" + text);
    } catch (Exception e) {
        e.printStackTrace();
    }
创建方法escapeUnicodeText以将Unicode文本转换为Unicode fromat,例如,如果传递输入字符串♫ 在escapeUnicodeText()中,您将获得unicode输出,如\u266b\u00e9

public String escapeUnicodeText(String input) {

    StringBuilder b = new StringBuilder(input.length());

    java.util.Formatter f = new java.util.Formatter(b);

    for (char c : input.toCharArray()) {
        if (c < 128) {
            b.append(c);
        } else {
            f.format("\\u%04x", (int) c);
        }
    }

    return b.toString();
}

在Android的URL编码中,@xerotolerant…可能是重复的…在编码之后,它是这样来的-ĐT70%2C+Cháu+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Há+Tĩnh%2C+Vietnam..虽然我想用这种格式-\U0111\U01b0\u1edng\U0110\u1eapp,Th\U00f4ng T\U1ef1,T\U00f9ng\U1ea2nh\U0110\U1ee9c Th\U1ecd,H\U00e0 T\U0129nh,越南。接下来的内容对我来说似乎是正确的。您要求的格式似乎不正确。您能解释一下为什么要这样做吗?因为对于ios,该字符串默认转换为\U0111\U01b0\u1edng\U0110\u1eapp、Th\U00f4ng T\U1ef1、T\U00f9ng\U1ea2nh、U0110\U1ee9c Th\U1ecd、H\U00e0 T\U0129nh、越南格式。对于android来说,它没有…还有一件事,当通过ĐT70%2C+Cháu+Linh%2C+tùng时+Ảnh%2C+Đức+Thọ%2C+Há+Tĩnh%2C+Vietnam。在url中,我没有得到正确的地址作为响应。@Abhishek使用这个,它将提供您期望的想要的输出。
 public String getUnicodeString(String myString) {
    String text = "";
    try {

         byte[] utf8Bytes = myString.getBytes("UTF8");
        text = new String(utf8Bytes, "UTF8");

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return text;
}