在twitter api请求中获取错误代码消息(java)

时间:2012-06-30 12:53:57

标签: java json api twitter

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

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

1 个答案:

答案 0 :(得分: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));