【问题标题】:Getting the error code message in twitter api request (java)在 twitter api 请求中获取错误代码消息(java)
【发布时间】:2012-06-30 12:53:57
【问题描述】:

我正在尝试通过以下方式使用 twitter api:

String urlAdd = "https://api.twitter.com/1/following/ids.json?user_id=1000123";
URL url = new URL(urlAdd);
URLConnection urlConnection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

getInputStream 输入流抛出 IOException,这是因为我已达到请求限制。 我希望能够区分请求限制错误和其他错误。 Twitter 以 json 格式返回错误消息,但由于抛出异常,我无法阅读。

关于如何获取错误消息的任何想法?

【问题讨论】:

  • JSON 在 Java 中的实现很好。假设您可以从 IOException 中获取 JSON,您可以使用 JSON 解析器对其进行解析并阅读错误消息。另一方面,您可以将Java APIs 之一用于 Twitter。
  • @skwee IOException 返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400,这不是我想要的。

标签: java json api twitter


【解决方案1】:

我找到了一种方法:

String urlAdd = "https://api.twitter.com/1/following/ids.json?user_id=1000123";
URL url = new URL(urlAdd);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
InputStream is;
if (httpConn.getResponseCode() >= 400) {
    is = httpConn.getErrorStream();
} else {
    is = httpConn.getInputStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(is));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多