Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取TwitterAPI请求中的错误代码消息(java)_Java_Json_Api_Twitter - Fatal编程技术网

获取TwitterAPI请求中的错误代码消息(java)

获取TwitterAPI请求中的错误代码消息(java),java,json,api,twitter,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(urlConn

我尝试以下列方式使用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));

我找到了一种方法:

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中实现是很好的。假设可以从IOException获取JSON,则可以使用JSON解析器对其进行解析并读取错误消息。另一方面,您可以为Twitter使用其中一个。@skwee IOException返回java.io.IOException:Server为URL返回HTTP响应代码:400,这不是我想要的。在Java中实现是很好的。假设可以从IOException获取JSON,则可以使用JSON解析器对其进行解析并读取错误消息。另一方面,您可以为Twitter使用其中一个。@skwee IOException返回java.io.IOException:Server为URL返回HTTP响应代码:400,这不是我想要的。