StackExchange API 中的 JSON URL 返回乱码?
我有一种感觉,我在这里做错了什么,但我不太确定我是否错过了一个步骤,或者只是遇到了编码问题或其他问题。这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种情况就不是字符编码问题,而是内容编码问题;您需要文本,但服务器正在使用压缩来节省带宽。如果您在抓取该 url 时查看标头,您可以看到您正在连接的服务器正在返回 gzipped 内容:
因此,您要么需要使用更智能的客户端,如 Apache 的 HttpClient,如 stevedbrown 建议的那样(尽管您需要 进行调整以使其自动使用 Gzip),或者显式解压缩示例代码中获得的流。请在声明输入的行尝试此操作:
我已验证这适用于您尝试获取的网址。
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:
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:
I've verified that this works for the url you are trying to grab.
请改用 Apache Http Client,它将正确处理字符转换。来自 该站点的示例:
在本例中,请参阅 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:
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.
有时 API 调用响应会被压缩,例如。 StackExchange API。请仔细阅读他们的文档并检查他们正在使用的压缩。有些使用 GZIP 或 DEFLATE 压缩。如果使用 GZIP 压缩,请使用以下内容。
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.