堆栈交换oauth响应不可读

时间:2017-10-18 16:22:51

标签: java oauth gzip stackexchange-api oltu

您好我正在尝试基于Java和apache Oltu库建立一个授权oauth客户端,用于堆栈交换api。

我收到的回复在控制台中打印时看起来像这样       un0" + TUL * KLۄ{BԨMshW#ڇWJ @ٹ˚F* P N р =ѹ \k

在浏览器中,响应很好地以READABLE格式打印。我的问题完全相同 Http request to stackexchange api returns unreadable json 但正如接受的答案所示,响应流是gziped JSON。当我尝试解压缩响应时,我的程序说“不是GZIP格式”格式。我试过以下: 新的GZIPInputStream(响应)

如果我将响应转换为十六进制,我会得到以下结果:

0x1FEFBFBD080000000000040075EFBFBDEFBFBD6EEFBFBD3010EFBFBD5FEFBFBDEFBFBD222B140CEFBFBDEFBFBDEFBFBD7455EFBFBDEFBFBD6CEFBFBDEFBFBD2A6BEFBFBD07EFBFBD156C13DB84EFBFBDEFBFBD7B0742D4A84DEFBFBDEFBFBDEFBFBDEFBFBD73EFBFBD68EFBFBD57EFBFBD23EFBFBDEFBFBDDA8757EFBFBD086A40D9B9EFBFBD462A50EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD1216EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD3C61EFBFBD1B1505D78441EFBFBDDF95EFBFBDEFBFBDCE8B2627EFBFBDEFBFBD4433EFBFBD6E45EFBFBDEFBFBD610CEFBFBDEFBFBD1142EFBFBDEFBFBD29EFBFBD6B545241EFBFBD44455EEFBFBD4DEFBFBD45797052EFBFBD10EFBFBD69C99B42E49C8BEFBFBD79EFBFBDEFBFBD08513B2BEFBFBD0B58EFBFBDEFBFBD22EFBFBDEFBFBDEFBFBD33EFBFBD3CEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD71365EEFBFBD600BEFBFBDEFBFBD1748EFBFBDEFBFBD0F616DEFBFBDEFBFBDEFBFBDEFBFBDDBB5EFBFBD7321442EEFBFBD66EFBFBD3C07EFBFBD32EFBFBD13EFBFBDEFBFBD6BD0813CEFBFBD6207EFBFBD1756EFBFBDCDA9EFBFBDEFBFBD2DEFBFBD14EFBFBDEFBFBD35EFBFBD6D1BEFBFBD3B66EFBFBD1FEFBFBDEFBFBDEFBFBDEFBFBD1D7A0376EFBFBDEFBFBD515BEFBFBDEFBFBD2E314EEFBFBD4DEFBFBD10EFBFBD7B72EFBFBD30E FBFBDEFBFBDEFBFBDCE9974EFBFBD0FE9A1BD67474CCD9377EFBFBD1E516A03037E5059EFBFBDEFBFBD76EFBFBDEFBFBD0211EFBFBD2EEFBFBD16EFBFBD45EFBFBDEFBFBDEFBFBDEFBFBD2EEFBFBD38EFBFBD42EFBFBD05EFBFBD725E6156EFBFBDE9B1A8EFBFBDEFBFBD7B38677973EFBFBDEFBFBD5AEFBFBDEFBFBDEFBFBD73EFBFBDC69FEFBFBDEFBFBD68EFBFBDEFBFBD611A61EFBFBD16EFBFBD36E8A38DEFBFBDCF845D20EFBFBD4BEFBFBDEFBFBDEFBFBDEFBFBD797611EFBFBDEFBFBD5FEFBFBD164EEFBFBDEFBFBDD180EFBFBDEFBFBD0EEFBFBD3DD1B9EFBFBD015C1A6BEFBFBDEFBFBD020000

所以现在我的问题是我无法直接读取JSON并且无法使用GZIP解压缩它。我现在可以做些什么来处理数据?有任何想法吗 ?感谢您的时间。

2 个答案:

答案 0 :(得分:1)

到目前为止,我写了一个这样的直接http调用// HTTP GET请求     private String sendGet(String accessToken)抛出异常{

    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;
    }

答案 1 :(得分:0)

你可以尝试OkHttp。这是一个适合我的例子。它会自动解决我的解压缩问题。

    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>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>