【问题标题】:JSON URL from StackExchange API returning jibberish?来自 StackExchange API 的 JSON URL 返回乱码?
【发布时间】:2010-05-22 15:24:10
【问题描述】:

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

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

当我打印内容时,我会得到一大堆的翅膀和特殊字符,基本上是乱码。我会在这里复制并过去,但这不起作用。我做错了什么?

【问题讨论】:

    标签: java json url stackexchange-api


    【解决方案1】:

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

    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>
    

    因此,您要么需要像 stevedbrown 建议的那样使用更智能的客户端,例如 Apache 的 HttpClient(尽管您需要 a tweak to get it to speak Gzip automatically),要么显式解压缩您在示例代码中获得的流。在声明输入的那一行试试这个:

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

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

    【讨论】:

      【解决方案2】:

      改用Apache Http Client,它将正确处理字符转换。来自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();        
      }
      

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

      【讨论】:

      • 这并不能解决解压缩 api.stackoverflow.com 返回的内容的问题。
      【解决方案3】:

      有时 API 调用响应会被压缩,例如。堆栈交换 API。请浏览他们的文档并检查他们使用的压缩。有些使用 GZIP 或 DEFLATE 压缩。如果是 GZIP 压缩,请使用以下内容。

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

      【讨论】:

        猜你喜欢
        • 2019-01-21
        • 2015-08-06
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多