我正在尝试下载包含斯洛文尼亚字符的json文件,在下载json文件作为字符串时,我将获得json数据中指定的特殊字符
- "send_mail": "Po�lji elektronsko sporocilo.","str_comments_likes": "Komentarji,v�ecki in mejniki",
我正在使用的代码
- URL url = new URL(f_url[0]);
- URLConnection conection = url.openConnection();
- conection.connect();
- try {
- InputStream input1 = new BufferedInputStream(url.openStream(),300);
- String myData = "";
- BufferedReader r = new BufferedReader(new InputStreamReader(input1));
- StringBuilder totalValue = new StringBuilder();
- String line;
- while ((line = r.readLine()) != null) {
- totalValue.append(line).append('\n');
- }
- input1.close();
- String value = totalValue.toString();
- Log.v("To Check Problem from http paramers",value);
- } catch (Exception e) {
- Log.v("Exception Character Isssue","" + e.getMessage());
- }
我想知道如何正确下载字符.
解决方法
您需要将字符串字节编码为UTF-8.请检查以下代码:
- String slovenianJSON = new String(value.getBytes([Original Code]),"utf-8");
- JSONObject newJSON = new JSONObject(reconstitutedJSONString);
- String javaStringValue = newJSON.getString("content");
我希望它会对你有所帮助!