gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:39 25 4
gpt4 key购买 nike

我尝试通过以下方式使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com