1

我正在尝试通过以下方式使用 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 格式返回错误消息,但由于抛出异常,我无法阅读它。

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

4

1 回答 1

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));
于 2012-06-30T17:38:00.607 回答