Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download String file which contain special characters of slovenia

I am trying to download the json file which contains slovenian characters,While downloading json file as a string I am getting special character as specified below in json data

"send_mail": "Po�lji elektronsko sporocilo.",
"str_comments_likes": "Komentarji, v�ecki in mejniki",

Code which I am using

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());
}

I want to know how to get characters downloaded properly.

like image 449
Rakesh Avatar asked Dec 20 '17 11:12

Rakesh


2 Answers

You need to encode string bytes to UTF-8. Please check following code :

String slovenianJSON = new String(value.getBytes([Original Code]),"utf-8");
JSONObject newJSON = new JSONObject(reconstitutedJSONString);
String javaStringValue = newJSON.getString("content");

I hope it will help you!

like image 70
Yuichi Akiyoshi Avatar answered Sep 21 '22 00:09

Yuichi Akiyoshi


Decoding line in while loop can work. Also you should add your connection in try catch block in case of IOException

URL url = new URL(f_url[0]);
try {
    URLConnection conection = url.openConnection();
    conection.connect();
    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) {
        line = URLEncoder.encode(line, "UTF8");
        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());
}
like image 25
Ergin Ersoy Avatar answered Sep 21 '22 00:09

Ergin Ersoy