我正在尝试通过以下方式使用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格式返回错误消息,但由于抛出异常,我无法读取它。
关于如何获取错误消息的任何想法?
最佳答案
我找到了一种方法:
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));
关于java - 在Twitter API请求中获取错误代码消息(Java),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11274229/