The speed does not change from an asynchronous request



I need to make about 60 HTTP requests.

In the first case, I did not use an asynchronous request and the speed was about 1.5 minutes. In the second case, I used an asynchronous request and the speed did not change either and was about 1.5 minutes.

Please see my code. Maybe I’m not doing the asynchronous request correctly or is there some other way to quickly make a lot of HTTP requests?

public class Main {

    public static int page = 0;

    public static void main(String[] args) throws IOException {
        int totalPages = Util.getTotalPages();

        page = 0;
        while(page < totalPages) {
            // Function does not work
            new GetAuctions();
            page++;
        }
    }
}
public class Util {
    public static final String API_KEY = "***";
    public static final OkHttpClient httpClient = new OkHttpClient();
    public static final List<JSONObject> auctions = new ArrayList<>();

    public static int getTotalPages() throws IOException {
        Request request = new Request.Builder().url("https://api.hypixel.net/skyblock/auctions?key=" + Util.API_KEY + "&page=0").build();

        Response response = httpClient.newCall(request).execute();

        if (!response.isSuccessful()) throw new IOException("Error: " + response);

        assert response.body() != null;
        String jsonData = response.body().string();

        JSONObject object = new JSONObject(jsonData);

        return object.getInt("totalPages");
    }
}
public class GetAuctions {

    public static void main(String[] args) throws Exception {
        new GetAuctions().run();
    }

    public void run() throws Exception {
        Request request = new Request.Builder().url("https://api.hypixel.net/skyblock/auctions?key=" + Util.API_KEY + "&page=" + Main.page).build();

        Util.httpClient.newCall(request).enqueue(new Callback() {
            @Override public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                assert response.body() != null;
                String jsonData = response.body().string();

                JSONObject object = new JSONObject(jsonData);
                JSONArray array = object.getJSONArray("auctions");

                for (int i=0; i<array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);

                    if (jsonObject.has("bin")) {
                        Util.auctions.add(jsonObject);
                    }
                }

                System.out.println(Util.auctions.size());
            }
        });
    }
}

Answer

It doesn’t look like your example is asynchronous at all. Look at the example from https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/AsynchronousGet.java and try with that.

Specifically you should be calling enqueue instead of execute.

    client.newCall(request).enqueue(new Callback() {
      @Override public void onFailure(Call call, IOException e) {
        e.printStackTrace();
      }

      @Override public void onResponse(Call call, Response response) throws IOException {
        try (ResponseBody responseBody = response.body()) {
          if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

          Headers responseHeaders = response.headers();
          for (int i = 0, size = responseHeaders.size(); i < size; i++) {
            System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
          }

          System.out.println(responseBody.string());
        }
      }
    });
  }


Source: stackoverflow