javaoauthgzipstackexchange-apioltu

Stack exchange oauth response unreadable


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.


Solution

  • 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>