android – 如何下载包含斯洛文尼亚特殊字符的字符串文件

前端之家收集整理的这篇文章主要介绍了android – 如何下载包含斯洛文尼亚特殊字符的字符串文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试下载包含斯洛文尼亚字符的json文件,在下载json文件作为字符串时,我将获得json数据中指定的特殊字符
  1. "send_mail": "Po�lji elektronsko sporocilo.","str_comments_likes": "Komentarji,v�ecki in mejniki",

我正在使用的代码

  1. URL url = new URL(f_url[0]);
  2. URLConnection conection = url.openConnection();
  3. conection.connect();
  4. try {
  5. InputStream input1 = new BufferedInputStream(url.openStream(),300);
  6. String myData = "";
  7. BufferedReader r = new BufferedReader(new InputStreamReader(input1));
  8. StringBuilder totalValue = new StringBuilder();
  9. String line;
  10. while ((line = r.readLine()) != null) {
  11. totalValue.append(line).append('\n');
  12. }
  13. input1.close();
  14. String value = totalValue.toString();
  15. Log.v("To Check Problem from http paramers",value);
  16. } catch (Exception e) {
  17. Log.v("Exception Character Isssue","" + e.getMessage());
  18. }

我想知道如何正确下载字符.

解决方法

您需要将字符串字节编码为UTF-8.请检查以下代码
  1. String slovenianJSON = new String(value.getBytes([Original Code]),"utf-8");
  2. JSONObject newJSON = new JSONObject(reconstitutedJSONString);
  3. String javaStringValue = newJSON.getString("content");

我希望它会对你有所帮助!

猜你在找的Android相关文章