0

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();
    }
nicholxs
  • 26
  • 1
  • You may want to have a look to `Future` and/or http libraries exposing an async API – Gaël J Aug 29 '21 at 13:02
  • The main answer did kind of answer my question, however I was not able to get it to work. Regarding the second answer, async-http-client, I'm not sure how I could go about using it to my needs based off its examples. – nicholxs Aug 30 '21 at 06:42

1 Answers1

-1

You could create multiple threads and each of them would be in charge of one request

94230
  • 146
  • 2
  • 12