1

Hello I am trying to set up an authorization oauth client based on Java and apache Oltu library for stack exchange api.

The response i received looks like this when printed in console u��n�0���"+���tU��l��*k��lۄ��{BԨM����s�h�W�#��ڇWj@ٹ�F*P�����������N��р���=ѹ�\k��

In browser the response is nicely printed in READABLE format. My problem is exactly similar as Http request to stackexchange api returns unreadable json but as the accepted answer suggests the response stream is gziped JSON. When I try to unzip the response my program says "Not in GZIP format" format. I tried following: new GZIPInputStream(response)

If i convert the response to hex i get the following:

0x1FEFBFBD080000000000040075EFBFBDEFBFBD6EEFBFBD3010EFBFBD5FEFBFBDEFBFBD222B140CEFBFBDEFBFBDEFBFBD7455EFBFBDEFBFBD6CEFBFBDEFBFBD2A6BEFBFBD07EFBFBD156C13DB84EFBFBDEFBFBD7B0742D4A84DEFBFBDEFBFBDEFBFBDEFBFBD73EFBFBD68EFBFBD57EFBFBD23EFBFBDEFBFBDDA8757EFBFBD086A40D9B9EFBFBD462A50EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD1216EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD3C61EFBFBD1B1505D78441EFBFBDDF95EFBFBDEFBFBDCE8B2627EFBFBDEFBFBD4433EFBFBD6E45EFBFBDEFBFBD610CEFBFBDEFBFBD1142EFBFBDEFBFBD29EFBFBD6B545241EFBFBD44455EEFBFBD4DEFBFBD45797052EFBFBD10EFBFBD69C99B42E49C8BEFBFBD79EFBFBDEFBFBD08513B2BEFBFBD0B58EFBFBDEFBFBD22EFBFBDEFBFBDEFBFBD33EFBFBD3CEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD71365EEFBFBD600BEFBFBDEFBFBD1748EFBFBDEFBFBD0F616DEFBFBDEFBFBDEFBFBDEFBFBDDBB5EFBFBD7321442EEFBFBD66EFBFBD3C07EFBFBD32EFBFBD13EFBFBDEFBFBD6BD0813CEFBFBD6207EFBFBD1756EFBFBDCDA9EFBFBDEFBFBD2DEFBFBD14EFBFBDEFBFBD35EFBFBD6D1BEFBFBD3B66EFBFBD1FEFBFBDEFBFBDEFBFBDEFBFBD1D7A0376EFBFBDEFBFBD515BEFBFBDEFBFBD2E314EEFBFBD4DEFBFBD10EFBFBD7B72EFBFBD30EFBFBDEFBFBDEFBFBDCE9974EFBFBD0FE9A1BD67474CCD9377EFBFBD1E516A03037E5059EFBFBDEFBFBD76EFBFBDEFBFBD0211EFBFBD2EEFBFBD16EFBFBD45EFBFBDEFBFBDEFBFBDEFBFBD2EEFBFBD38EFBFBD42EFBFBD05EFBFBD725E6156EFBFBDE9B1A8EFBFBDEFBFBD7B38677973EFBFBDEFBFBD5AEFBFBDEFBFBDEFBFBD73EFBFBDC69FEFBFBDEFBFBD68EFBFBDEFBFBD611A61EFBFBD16EFBFBD36E8A38DEFBFBDCF845D20EFBFBD4BEFBFBDEFBFBDEFBFBDEFBFBD797611EFBFBDEFBFBD5FEFBFBD164EEFBFBDEFBFBDD180EFBFBDEFBFBD0EEFBFBD3DD1B9EFBFBD015C1A6BEFBFBDEFBFBD020000

So my issue now is that I can not read the JSON directly and can not unzip it using GZIP. What can I do now to process the data? Any ideas ? Thank you for your time.

Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
Arka Mallick
  • 1,206
  • 3
  • 15
  • 28

2 Answers2

1

As of now I wrote a direct http call like this // HTTP GET request private String sendGet(String accessToken) throws Exception {

    String url = "https://api.stackexchange.com/2.2/me?site=stackoverflow&key="key"((&access_token=";
    url = url + accessToken;
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");      
    int responseCode = con.getResponseCode();
    if(responseCode == 200){
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        //System.out.println(con.getContentEncoding());
        //System.out.println(con.getContentType());

        GZIPInputStream gp = new GZIPInputStream(con.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(gp));

        StringBuffer response = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            response.append(line);
        }

        br.close();

        System.out.println(response.toString());
        return response.toString();
    }
    else{
        return null;
    }
Arka Mallick
  • 1,206
  • 3
  • 15
  • 28
0

You can try OkHttp. Here is an example works for me. It automatically solves that decompression problem for me.

    UriComponents uriComponents =
            UriComponentsBuilder.newInstance()
                    .scheme("https")
                    .host("api.stackexchange.com")
                    .path("/2.2/users")
                    .queryParam("order", "desc")
                    .queryParam("sort", "reputation")
                    .queryParam("inname", fullName)
                    .queryParam("site", "stackoverflow")
                    .build()
                    .encode();

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(uriComponents.toUriString())
            .get()
            .addHeader("cache-control", "no-cache")
            .build();

    Response response = client.newCall(request).execute();
    return new JSONObject(response.body().string());

Maven dependency

    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>
Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
  • 1
    Thank you. I will try this out. As of now I wrote a direct http call like this – Arka Mallick Nov 14 '17 at 23:04
  • 1
    I noticed you did not have to hook up ur key as part of the url http request. For me I seem to need it. Is it wrong? As of now I wrote a direct http call like this. Oh had to add the code as an answer. Could not add as comment. too long. – Arka Mallick Nov 14 '17 at 23:11
  • It depends which endpoint you are using. You can see some endpoints need that key in the following link: https://api.stackexchange.com/docs – Furkan Yavuz Nov 16 '17 at 12:40