6

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.

Ergin Ersoy
  • 890
  • 8
  • 28
Rakesh
  • 14,997
  • 13
  • 42
  • 62
  • have a look at [This question](https://stackoverflow.com/questions/15737554/issue-while-downloading-file-with-special-character) if you haven't already . – ADM Dec 20 '17 at 11:10
  • Possible duplicate of [Decoding UTF-8 String, then encoding it in 8859-2 for Slovakian alphabet](https://stackoverflow.com/questions/46521867/decoding-utf-8-string-then-encoding-it-in-8859-2-for-slovakian-alphabet) – Ramesh sambu Dec 20 '17 at 11:13
  • I tried changing to UTF-8 or 8859-2 ,nothing works,only change what i see is 8859-2 character changes from � to [] . – Rakesh Dec 22 '17 at 04:07
  • I'm confused- where is the actual JSON? You're not using any Android or Java JSON library – ubadub Dec 26 '17 at 21:16
  • @Rakesh r u using retrofit library ?? – chandani c patel Jan 01 '18 at 07:58
  • I am not using retrofit library and i updated the answer – Rakesh Jan 09 '18 at 07:02

7 Answers7

4

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!

Yuichi Akiyoshi
  • 409
  • 5
  • 19
2

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());
}
Ergin Ersoy
  • 890
  • 8
  • 28
2

It's not entirely clear why you're not using Android's JSONObject class (and related classes). You can try this, however:

String str = new String(value.getBytes("ISO-8859-1"), "UTF-8");

But you really should use the JSON libraries rather than parsing yourself

ubadub
  • 3,571
  • 21
  • 32
1

When creating the InputStreamReader at this line:

       BufferedReader r = new BufferedReader(new InputStreamReader(input1));

send the charset to the constructor like this:

       BufferedReader r = new BufferedReader(new InputStreamReader(input1), Charset.forName("UTF_8"));
1

problem is in character set

as per Wikipedia Slovene alphabet supported by UTF-8,UTF-16, ISO/IEC 8859-2 (Latin-2). find which character set used in server, and use the same character set for encoding.

if it is UTF-8 encode like this

       BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream), Charset.forName("UTF_8"));

if you had deffrent character set use that.

Anu Martin
  • 711
  • 1
  • 9
  • 20
1

I have faced same issue because of the swedish characters.

So i have used BufferedReader to resolved this issue. I have converted the Response using StandardCharsets.ISO_8859_1 and use that response. Please find my answer as below.

    BufferedReader r = new BufferedReader(new InputStreamReader(response.body().byteStream(), StandardCharsets.ISO_8859_1));
    StringBuilder total = new StringBuilder();
    String line;

    while ((line = r.readLine()) != null) 
    {
        total.append(line).append('\n');
    }

and use this total.toString() and assigned this response to my class.

I have used Retrofit for calling web service.

Chirag
  • 56,621
  • 29
  • 151
  • 198
0

I finally found this way which worked for me

InputStream input1 = new BufferedInputStream(conection.getInputStream(), 300);

BufferedReader r = new BufferedReader(new InputStreamReader(input1, "Windows-1252"));

I figured out by this windows-1252, by putting json file in asset folder of the android application folder, where it showed same special characters like specified above,there it showed auto suggestion options to change encoding to UTF-8,ISO-8859-1,ASCII and Windows-1252, So I changed to windows-1252, which worked in android studio which i replicated the same in our code, which worked.

Deˣ
  • 4,191
  • 15
  • 24
Rakesh
  • 14,997
  • 13
  • 42
  • 62