8

I need to make multiple calls to API REST with Retrofit and show the response in a ListView, but I don't know how to do this and this code doesn't work.

Model

@GET("apks/{sha256}")
    Call<DatoAPI> getTask2(@Path("sha256") String hash, @Query("Authorization") String key);

Implementation

for (String s: hash) {                                          
    Call<DatoAPI> call = services.getTask2(s, API.API_KEY);
    call.enqueue(new Callback<DatoAPI>() {
        @Override
        public void onResponse(Call<DatoAPI> call, Response<DatoAPI> response) {
            if (response.isSuccessful()) {
                datoAPI = response.body();
                items.add(datoAPI.getApp());
            }
        }

        @Override
        public void onFailure(Call<DatoAPI> call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
}

Also I tried with call.execute() and same problem I want to show this response in a ListView but it doesn't work.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Daniel Díaz
  • 186
  • 1
  • 2
  • 12

1 Answers1

12

First of all you need to understand the differences between Retrofit's Call#enqueue() and Call#execute() methods.

  1. enqueue() method is Asynchronous which means you can move on to another task before it finishes

  2. execute() method is Synchronous which means, you wait for it to finish before moving on to another task.

And in your case, you're using for loop to execute multiple requests in a single stretch.

Now, if you use for loops to execute network operation, the network operation will not stop for loops from going to the next iteration. Do not expect that the API will always respond in a fast enough way before going to for loops next iteration. That's a bad idea.

If you use Retrofit's execute() method, it will not allow you to continue to next line (or iteration) as its Synchronous behavior, plus it throws NetworkOnMainThreadException and IOException. Hence, you need to wrap the request in an AsyncTask and handle IOException.

I'd recommend you to use RxAndroid with RxJava instead of using for loops. There are plenty of tutorials out there on this topic.

Refer to the following StackOverflow questions to solve your problem.

  1. How to make multiple request and wait until data is come from all the requests in Retrofit 2.0 - Android?
  2. Asynchronous vs synchronous execution, what does it really mean?

Adjust the code as per your requirements.

Good luck!

Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • I'm a novice in API rest consumption and I don't get that StackOverflow questions, can you help me to get it? – Daniel Díaz Sep 10 '18 at 17:45
  • If you're novice, don't go for advanced things now. Learn basic things first. Start learning how basic API requests works instead of starting with multiple requests. Understand the core features first. I'm not discouraging you to not learn advanced things and I'm also not forcing you to refer SO questions. When you Google for things you get tones of results. This is how we learnt and learning more new technologies. And keep in mind that SO is not discussion forum. Start learning, (re)search about it. If you don't find solutions for the errors ask new question here, fellow community usrs help u – Shashanth Sep 10 '18 at 18:07
  • 1
    Im just novice in API rest, I learned a simple request and retrofit, I just need to know how to make multiple request. – Daniel Díaz Sep 10 '18 at 18:20
  • Whatever you tried is not a good practice (using for loops). I strongly recommend RXJava2 and RXAndroid. As these technology makes your requirement implementation path a lot easier. There are tones of tutorials out there. To learn basics of RxJava, start from here [AndroidHive](https://www.androidhive.info/RxJava/android-getting-started-with-reactive-programming/). Good luck! – Shashanth Sep 11 '18 at 04:42
  • 1
    Thanks you so much, I'm going to search in AndroidHive. – Daniel Díaz Sep 11 '18 at 14:34