如何解码Stack Exchange API响应 [英] how to decode the Stack Exchange API response

查看:78
本文介绍了如何解码Stack Exchange API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索堆栈交换API的响应,例如 [ http://api.stackexchange.com/2.2/tags?order = desc& sort = popular& site = stackoverflow]

I am trying to retrieve the response of stack exchange api like [http://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow]

我正在使用以下代码来检索响应

I am using the following code to retrieve the response

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class RetrieveAllTag {

    public static void main(String... args) {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow");
        HttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);
            InputStream content = response.getEntity().getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(content,"UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            String inputLine;
            while ((inputLine = reader.readLine()) != null) {
                stringBuilder.append(inputLine);
                stringBuilder.append("\n");
            }
            System.out.println(stringBuilder.toString());

        }
        catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        finally {
            httpClient.getConnectionManager().shutdown();
        }
    }
}

但是我得到的解码形式的响应为 ߅\ f]按 DՊ I/m.( * Ʃ Kc .

But I am getting the response in decoded form as ���n� �߅\f]as��DՊ�I��/�m�(��*Ʃ���Kc���

我发现了类似的问题[ https://stackoverflow.com /questions/20808901/problems-with-decoding-stack-exchange-api-response] , 但是我没有找到这个问题的答案.

I found the similar question [https://stackoverflow.com/questions/20808901/problems-with-decoding-stack-exchange-api-response] , but I didn't find any answers for the question.

如何解码api响应?

谢谢.

推荐答案

内容已压缩.您需要通过解压缩流将其发送,例如

The content is compressed. You need to send it through an unzip stream, like

import java.util.zip.GZIPInputStream;

...
InputStream content = response.getEntity().getContent();
content = new GZIPInputStream(content);
...

您还应该首先检查内容编码,并且如果编码实际上是 gzip,则仅将流包装到GZIPInputStream中-一些代理已经透明地解压缩了流.

You should also check the content encoding first, and only wrap the stream into a GZIPInputStream if the encoding actually is gzip - some proxies transparently already uncompress the stream.

请参见 SOQuery.java java.net.HttpURLConnection而不是apache客户端,也可以使用a>以获得完整的示例.

See SOQuery.java for a complete sample, even though this is using java.net.HttpURLConnection rather than the apache client.

这篇关于如何解码Stack Exchange API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆