0

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

2
  • Might be easier to use an actual HTTP library; perhaps it's gzipped? Impossible to say since you don't provide an example of the output value. Commented Mar 29, 2017 at 16:17
  • I edited my post by adding a picture of my output. Commented Mar 29, 2017 at 16:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I do not think it is a gZip. You can open this link "api.stackexchange.com/2.2/…" in your browser.
@Paul It turns out browsers handle un-gzipping content. It's kind of how the web manages to work. For example, if you open the link from your question in the browser and look at the network request, you'll see this: Content-Encoding: gzip. So.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.