To begin, I should note that I'm pretty new to Java, so doing research on this topic has quite a learning curve, and I hardly understand what I am reading. My intended goal is to send multiple HTTP GET requests (~60) and then combine all of the data into a JSONObject in a reasonable timeframe.
I have managed to partially pull off the task (though my methods are likely unorthodox), however, it was synchronous which left me waiting for quite a while before I received my response which is not what I'm aiming to achieve.
If anyone could provide some insight on how I can approach this, it'll be of great help, as well as where I can learn more about the parts being used.
private static int pages;
private static final String BASE = "https://someapi.com/api?page=";
public static String getData() throws IOException {
JSONObject finalData = new JSONObject();
System.out.println("Attempting to get API data!");
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(
BASE + "0");
HttpResponse response = client.execute(request);
String result = IOUtils.toString(new BufferedReader
(new InputStreamReader(
response.getEntity().getContent())));
JSONObject jsonData = new JSONObject(result);
pages = jsonData.getInt("totalPages");
totalData.put("0", jsonData.getJSONArray("data"));
for (int x = 1; x < pages; x++) {
totalData.put(Integer.toString(x), getPageData(x)); // getPageData is essentially a copy-paste of the HTTP client except it has a different number being added to the paste.
}
return totalData.toString();
}