Search code examples
javajsonresthttpresponsestackexchange

Reading REST Request Output using HttpURLConnection


I am trying to do REST request on the StackExchange API on java and I have some problems to read the output. I made a small code example to see if I can read properly:

package Utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestClient
{


public static void main(String[] args)
{
    try
    {
        URL url = new URL("https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        System.out.println(conn.getResponseCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String output;

        while ((output = reader.readLine()) != null)
        {
            System.out.println(output);
        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

}

Below is a sample output from the website:

{"items":[{"tags":["c++","templates","covariant"],"owner":{"reputation":3,"user_id":7447292,"user_type":"registered","profile_image":"https://www.gravatar.com/avatar/8979f1b328b9b0f786dec8b4edd514bc?s=128&d=identicon&r=PG&f=1","display_name":"B.Oudot","link":"http://stackoverflow.com/users/7447292/b-oudot"}

However when I print the output of my java program, I get the status code 200 and then the rest is just bunch of non-ascii characters. I am new with REST and JSON. I would like to not use any 3rd party library if possible.

EDIT I have put the output of my program as an image.

Non Ascii Chatacters


Solution

  • you need to use GZIPInputStream api, when the content encoding used gZip. it is also an inputstream.

    i used gipinput stream to your response and i see output in json.

    public static void main(String[] args){
        try
        {
            URL url = new URL("https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            System.out.println(conn.getResponseCode());
    
    
    
            GZIPInputStream gzis = new GZIPInputStream(conn.getInputStream());
            InputStreamReader reader = new InputStreamReader(gzis);
            BufferedReader in = new BufferedReader(reader);
    
            String readed;
            while ((readed = in.readLine()) != null) {
                System.out.println(readed);
            }
            //BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
            String output;
    
            while ((output = in.readLine()) != null)
            {
                System.out.println(output);
            }
    
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    
        }
    

    it worked for me.