There are 5 id in idList . I need to add these id to requests to requests via retrofit. Each such request should give me a unique Product.

As a result, I want to get 5 Products in the same order as id in idList , add them to the products and send them to the adapter.

Here I am a miracle, but I write an error on the loadData() inside loadData (). As I understood, calling the loadData method again within the same method is not the most correct solution.

Tell me how to do it right.

 ArrayList<String> idList = new ArrayList(); //5 id товаров currentProductId = 0; // счетчик товаров, когда счетчик будет больше чем товаров в idList, //тогда добавлять товары в адаптер и выводить их. List<Products> products = new ArrayList<>(); private void loadData() { запрос, currentProductId.enqueue { if (idList != currentProductId) { products.add(response.body()); currentProductId++; loadData(); //тут ошибка, java.lang.IndexOutOfBoundsException } if (idList == currentProductId) { adapter.setItems(products); } } } 
  • Consider rxjava - katso
  • Sorry, but now my brain will not be mastered by understanding rxjava either, it’s necessary by standard means, and I’m going to stagnate with them, then I’ll learn new things - Sergey Value
  • And your server does not accept multiple id in one request - it would be easier and better, even if the order is not guaranteed (sort the trivia). You cited such a code that makes it difficult to understand exactly where your mistake is, one thing is clear - that the end of the list is incorrectly checked, you need to put this check right when entering the method, I guess. Not sure, of course, but the name of the enqueue method hints that requests are processed in turn, you can simply try to start them in a loop, and you can add one at a time to the adapter. - woesss
  • this is the problem that the server only sends goods one by one, one product per request. You can do this: for (int i = 0; i <idList.size (); i ++) {loadData (i); } But then the order of goods is lost because requests are executed randomly. - Sergey Value
  • Retrofit has a synchronous request square.imtqy.com/retrofit/2.x/retrofit/retrofit2/… , but then the cycle will have to be wrapped in the background thread itself. - woesss

1 answer 1

If you try to fix your code, it should be something like this:

 ArrayList<String> idList = new ArrayList(); //5 id товаров currentProductId = 0; // счетчик товаров, когда счетчик будет больше чем товаров в idList, //тогда добавлять товары в адаптер и выводить их. List<Products> products = new ArrayList<>(); private void loadData() { if (currentProductId >= idList.size()) { adapter.setItems(products); return; } //запрос, currentProductId.enqueue, onResponse { products.add(response.body()); currentProductId++; loadData(); // } 
  • The code works and, in my case, it issues the goods in turn (using the .enqueue request). It's cool that my mind moved and you showed me that you can do a check inside the method before executing the query, thank you so much. Spent a week on this piece of code, but solved the problem, eee - Sergey Value
  • one
    @SergeyValue, if the answer helped solve your problem, mark it as correct (tick next to the answer) - Jarvis_J