StackExchange API 中的 JSON URL 返回乱码?

发布于 2024-09-02 17:34:18 字数 516 浏览 8 评论 0原文

我有一种感觉,我在这里做错了什么,但我不太确定我是否错过了一个步骤,或者只是遇到了编码问题或其他问题。这是我的代码:

URL url = new URL("http://api.stackoverflow.com/0.8/questions/2886661");

   BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
   // Question q = new Gson().fromJson(in, Question.class);
   String line;
   StringBuffer content = new StringBuffer();
   while ((line = in.readLine()) != null)
   {
    content.append(line);
   }

当我打印内容时,我得到一大堆翅膀和特殊字符,基本上是胡言乱语。我想将其复制并粘贴到此处,但这不起作用。我做错了什么?

I have a feeling I'm doing something wrong here, but I'm not quite sure if I'm missing a step, or am just having an encoding problem or something. Here's my code:

URL url = new URL("http://api.stackoverflow.com/0.8/questions/2886661");

   BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
   // Question q = new Gson().fromJson(in, Question.class);
   String line;
   StringBuffer content = new StringBuffer();
   while ((line = in.readLine()) != null)
   {
    content.append(line);
   }

When I print content, I get a whole bunch of wingdings and special characters, basically jibberish. I would copy and past it here, but that isn't working. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-09-09 17:34:18

这种情况就不是字符编码问题,而是内容编码问题;您需要文本,但服务器正在使用压缩来节省带宽。如果您在抓取该 url 时查看标头,您可以看到您正在连接的服务器正在返回 gzipped 内容:

GET /0.8/questions/2886661 HTTP/1.1
Host: api.stackoverflow.com

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 22 May 2010 15:51:34 GMT
Content-Type: application/json; charset=utf-8
<more headers>
Content-Encoding: gzip
<more headers>

因此,您要么需要使用更智能的客户端,如 Apache 的 HttpClient,如 stevedbrown 建议的那样(尽管您需要 进行调整以使其自动使用 Gzip),或者显式解压缩示例代码中获得的流。请在声明输入的行尝试此操作:

 BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(url.openStream())));

我已验证这适用于您尝试获取的网址。

In this case it's not a character encoding problem, it's a content encoding problem; you're expecting text, but the server is using compression to save bandwidth. If you look at the headers when you grab that url, you can see the server you are connecting to is returning gzipped content:

GET /0.8/questions/2886661 HTTP/1.1
Host: api.stackoverflow.com

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 22 May 2010 15:51:34 GMT
Content-Type: application/json; charset=utf-8
<more headers>
Content-Encoding: gzip
<more headers>

So you either need to use a smarter client like Apache's HttpClient as stevedbrown suggests (although you need a tweak to get it to speak Gzip automatically), or explicitly decompress the stream you got in your example code. Try this instead for the line where you declare your input:

 BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(url.openStream())));

I've verified that this works for the url you are trying to grab.

可是我不能没有你 2024-09-09 17:34:18

请改用 Apache Http Client,它将正确处理字符转换。来自 该站点的示例

public final static void main(String[] args) throws Exception {

    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = 
        new HttpGet("http://api.stackoverflow.com/0.8/questions/2886661"); 

    System.out.println("executing request " + httpget.getURI());

    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);

    System.out.println("----------------------------------------");

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();        
}

在本例中,请参阅 http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/ httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java,它展示了如何处理Gzip内容。

Use the Apache Http Client instead, it's going to take care of character conversions properly. From that site's examples:

public final static void main(String[] args) throws Exception {

    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = 
        new HttpGet("http://api.stackoverflow.com/0.8/questions/2886661"); 

    System.out.println("executing request " + httpget.getURI());

    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);

    System.out.println("----------------------------------------");

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();        
}

In this case, see http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java, which shows how to deal with Gzip content.

萌梦深 2024-09-09 17:34:18

有时 API 调用响应会被压缩,例如。 StackExchange API。请仔细阅读他们的文档并检查他们正在使用的压缩。有些使用 GZIP 或 DEFLATE 压缩。如果使用 GZIP 压缩,请使用以下内容。

InputStream is = new URL(url).openStream();
BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(is)));

Sometimes the API call response are compressed eg. StackExchange API. Please go through their documentation and check for the compression they are using. Some use either GZIP or DEFLATE compression.In case of GZIP compression use the following.

InputStream is = new URL(url).openStream();
BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(is)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文