0

I have a function that makes API call to the server and updates the UI with new data. I want to make, so that every 30 seconds I make API call with different url each time ? And these API calls should be non-stop, as long as the app is running.

String url1 = "https://somewebsite.com/api/update-rates?locale=en";
String url2 = "https://somewebsite.com/api/update-rates?locale=ru";
String url3 = "https://somewebsite.com/api/update-rates?locale=fr";

public void getFreshRates (String url) {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

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

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if(response.isSuccessful()) {
                    String myResponse = response.body().string();
                     // Handling the response here
                     }

                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // Updating My UI here
                        }
                    });
                }
        });
    }
casper
  • 259
  • 4
  • 18
  • Is the key phrase here, "with a different parameter each time?" I.e., do you know how to have some function, `f` called with the _same_ parameter (or no parameter) each time? If so, then why not write a function, `f`, that first calculates the next parameter `p` to give to function `g`, and then calls `g(p)`? – Solomon Slow Mar 03 '22 at 16:30
  • Simply saying, I need to infinitely make api call each 30 seconds, and each time with different endpoint. (3 fixed endpoints) – casper Mar 04 '22 at 04:36

2 Answers2

0

To run every 30 seconds you can use the solution of this post: Running a Java Thread in intervals

And also, maybe you can use CompletableFuture to do asynchronous calls and retrieve the result:

CompletableFuture<Void> future1  
  = CompletableFuture.supplyAsync(() -> getFreshRates(url1));
CompletableFuture<Void> future2  
  = CompletableFuture.supplyAsync(() -> getFreshRates(url2));
CompletableFuture<Void> future3  
  = CompletableFuture.supplyAsync(() -> getFreshRates(url3));

CompletableFuture<Void> combinedFuture 
  = CompletableFuture.allOf(future1, future2, future3);
    
combinedFuture.get();

See: https://www.baeldung.com/java-completablefuture#Multiple

  • It's not exactly what im trying to achieve. The calls should be synchronous. I know how to call a function in intervals with ScheduledExecutorService. I just can't figure out how to switch between urls every 30 secs – casper Mar 03 '22 at 16:00
  • I think in this case you can use an infinite loop that has an array with the URLs and a session variable (index) to control which URL you will request. For each iteration, you can change the "index" by adding one and when the index is the same as the length of the array, you set it to zero. – Armando Almeida Mar 03 '22 at 16:54
0

Is it a fixed number of URLs? Because if it is, you can create N completable futures and run them indefinitely.

Aid Hadzic
  • 578
  • 3
  • 10