我正在使用Stackoverflow JSON API来检索标有给定标记的问题。
我在Java中有这个小程序,它检索标有"Java"标签的问题。public static void main(String[] args) throws Exception
{
String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow";
URL url = new URL( urlString );
BufferedReader reader = null;
StringBuffer buffer = new StringBuffer();
try
{
URLConnection connection = url.openConnection();
InputStream isConn = connection.getInputStream();
reader = new BufferedReader( new InputStreamReader( new GZIPInputStream( isConn ) ) );
String inputLine;
while (( inputLine = reader.readLine() ) != null)
{
buffer.append( inputLine );
}
}
finally
{
if (reader != null)
{
reader.close();
}
}
JSONObject jsonObject = new JSONObject( buffer.toString() );
JSONArray jsonArray = jsonObject.getJSONArray( "items" );
System.out.println( buffer );
System.out.println( jsonArray.length() );
}
我的问题是它只返回30个问题。因为我的目标是为进一步的文本分析建立一个数据集,所以我需要访问的不仅仅是30个问题。
是否有办法调整返回列表的大小?
如果不是,我该如何解决这种情况?
注意返回的JSON
中的has_more
属性,这表明有更多的结果可用。您可以使用url中的page
和pagesize
参数浏览这些结果。我预见到的问题是,考虑到代码将遍历所有java问题,它将拉出大量的问题,因此您可能想要添加一个在特定页数处停止的条件。下面是一个简单的例子:
public static void main(String[] args) throws Exception {
BufferedReader reader = null;
int page = 1;
JSONObject jsonObject = null;
try {
while (jsonObject == null || jsonObject.getBoolean("has_more")) {
String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow&pagesize=100";
urlString += "&page=" + page++;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
InputStream isConn = connection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(isConn)));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
buffer.append(inputLine);
}
jsonObject = new JSONObject(buffer.toString());
JSONArray jsonArray = jsonObject.getJSONArray("items");
System.out.println(buffer);
System.out.println(jsonArray.length());
}
} finally {
if (reader != null) {
reader.close();
}
}
}