A
A
A1994Y2020-05-15 14:23:32
Java
A1994Y, 2020-05-15 14:23:32

Response from server in encoding (u043a\u0430\u043a-\u0442\) how to decode?

I make a request like this:

HttpURLConnection urlConnection = null;

            try {
                urlConnection = (HttpURLConnection) new URL(urlConn).openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setUseCaches(false);
                urlConnection.connect();

                BufferedReader input = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UNICODE"));
                String line;
                while ((line = input.readLine()) != null){
                         sb.append(line);
                         sb.append("\n");
                }


At the output I see the required tag and in it "043a\u0430\u043a-\u0442\" incomprehensible characters, I understand the Unicode encoding, but how to get the Russian language instead of characters.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2020-05-15
@A1994Y

If you use https://www.online-toolz.com/tools/text-unicode-en... you can see that the data is "how-to"
Another point - new InputStreamReader(urlConnection.getInputStream(), "UNICODE") - InputStreamReader does not know such encoding as "UNICODE"
If you open the dock for this class, you can see that the correct charset value will be - "UTF-8"
PS: surprisingly, it didn't work. Helps

BufferedReader input = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8));
+
sb.append(StringEscapeUtils.unescapeJava(line));

X
X-nautilus, 2020-05-15
@X-nautilus

Try to set the encoding for the string

line=new String(line = input.readLine(), StandardCharsets.UTF_8)

I think it will help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question