Как починить сбитую кодировку при работе со Stack Overflow API

Волнует следующая проблема, мне необходимо распарсить из Stack OverFlow API JSON в строку, однако при вставке в строку бьется кодировка и различные getBytes(UTF-8) не помогают. Как это сделать правильно?

public class Main {

public static void main(String[] args) throws IOException {

    URL url = new URL("https://api.stackexchange.com/2.2/search?pagesize=100&order=desc&sort=creation&tagged=python&site=stackoverflow");

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", "Mozilla/5.0");

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;

    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    System.out.println(response);

}}

Ответы (1 шт):

Автор решения: wigravy
public static void main(String[] args) throws IOException {

            URL url = new URL("https://api.stackexchange.com/2.2/search?pagesize=100&order=desc&sort=creation&tagged=python&site=stackoverflow");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");

            GZIPInputStream gis = new GZIPInputStream(connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8));

            String inputLine;

            StringBuffer response = new StringBuffer();

            while ((inputLine = bufferedReader.readLine()) != null) {
                response.append(inputLine);
            }
            bufferedReader.close();
            System.out.println(response);
        }

Смотрим внимательно документацию или заголовки через Pоstman (Вы же проверяете запрос перед тем, как писать под него что-то?). В заголовках видим строку "content-encoding: gzip". И charset: UTF-8, собственно добавляем декодер и charset и вуаля, всё работает.

→ Ссылка