Need advice, there are 3 asynchronous threads:

The first stream (1) that starts it receives the amount of news on the site and runs only once

Feed the second (2) which gets the post content itself (id, content ... id_img)

The third stream (3) which receives the image of the post (which needs id_img from (2) the stream)

How to pass id_img from (2) stream to (3) stream?

Getting the total number of posts:

final int[] size = new int[1]; Call<ArrayList<PostModel>> call0 = api.getPost(100);//Тут получаем колличество постов call0.enqueue(new Callback<ArrayList<PostModel>>() { @Override public void onResponse(Call<ArrayList<PostModel>> call, Response<ArrayList<PostModel>> response) { size[0] = response.body().size(); } @Override public void onFailure(Call<ArrayList<PostModel>> call, Throwable t) { } }); 

Here we get the posts themselves

 Call<ArrayList<PostModel>> call = api.getPost(size[0]); //Получаем все посты(ID Изображения получаем тут же) call.enqueue(new Callback<ArrayList<PostModel>>() { @Override public void onResponse(Call<ArrayList<PostModel>> call, Response<ArrayList<PostModel>> response) { ArrayList<PostModel> post = response.body(); } @Override public void onFailure(Call<ArrayList<PostModel>> call, Throwable t) { Toast.makeText(getApplicationContext(), "Error load content", Toast.LENGTH_SHORT).show(); } }); 

And here we get the image by ID

 Call<ArrayList<ImageModel>> call1 = api.getImage(504);//Тут ID изображения call1.enqueue(new Callback<ArrayList<ImageModel>>() { @Override public void onResponse(Call<ArrayList<ImageModel>> call, Response<ArrayList<ImageModel>> response) { ArrayList<ImageModel> img = response.body(); } @Override public void onFailure(Call<ArrayList<ImageModel>> call, Throwable t) { } }); 

PS

 Call<ArrayList<ImageModel>> call1 = api.getImage(504) 

Instead of 504 there should be an id which we get along with the contents of the post in (2) stream

  • Where are the streams? All the code that is written here (callbacks) will be processed in the same stream in which it was created (UI most likely). - Eugene Krivenja
  • retrofit itself creates threads for itself (if asynchronous work is in progress), for synchronous work it is only necessary to create separate threads - Heaven
  • You do not need to think at all what is there and how it creates a retrofit in the case of asynchronous calls. Because here from documentation: On Android, callbacks will be executed on the main thread. On the JVM, callbacks will happen on the same thread that executed the HTTP request. On Android, callbacks will be executed on the main thread. On the JVM, callbacks will happen on the same thread that executed the HTTP request. - Eugene Krivenja
  • The callback is processed in the main thread, the request is sent in the background - Heaven

2 answers 2

All your question is essentially reduced to a simple task how to implement such a pattern.

 void a() { int result = // some calculation b(result); } void b(int resultFromA) { int resut = 2 * resultFromA; c(result); } void c(int resultFromB) { // do something with resultFromB value } 

The flows here are completely unnecessary.

  • Work with a network without streams? As far as I know, to work with the network it is necessary to allocate a separate background thread so that it does not load the UI, maybe I'm wrong? - Heaven
  • Retrofit is engaged in streams, in this is the chip of its asynchronous requests. You may need your own streams if any other operations with data other than network data may take some time. - Eugene Krivenja

Understood, wrote here such code:

The variable j is declared as global and is used for the counter (a small crutch), and it is possible that the code itself was not written correctly, but it works.

Maybe someone will help too

PS Immediately we are working with the database (Sugar ORM)

 Call<ArrayList<PostModel>> call = api.getPost(100);//Тут получаем все посты call.enqueue(new Callback<ArrayList<PostModel>>() { @Override public void onResponse(Call<ArrayList<PostModel>> call, final Response<ArrayList<PostModel>> response) { postORM = new ArrayList<>(); size = response.body().size(); for(int i = 0; i < response.body().size(); i++) { // Узнаем колличество постов postORM.add(new PostModelORM( //Добавляем посты в Базу данных Integer.parseInt(response.body().get(i).getIdPost()), response.body().get(i).getGuid().getRendered(), response.body().get(i).getDate(), response.body().get(i).getTitle().getRendered(), response.body().get(i).getContent().getRendered(), response.body().get(i).getExcerpt().getRendered(), response.body().get(i).getCategroiesMass())); SugarRecord.saveInTx(postORM);//Сохраняет пост в БД Call<ArrayList<ImageModel>> call1 = api.getImage(Integer.parseInt(response.body().get(i).getIdPost())); //Получаем изображение по ID поста call1.enqueue(new Callback<ArrayList<ImageModel>>() { @Override public void onResponse(Call<ArrayList<ImageModel>> call1, Response<ArrayList<ImageModel>> response1) { if(response1.body().size() == 1) { //Получаем одно изображение postORM.get(j).setUrl_img(response1.body().get(0).getGuid().getRendered()); }else if(response1.body().size() == 0) { //Нет изображений postORM.get(j).setUrl_img(""); }else{ for(int i = 0; i < response1.body().size(); i++) { //Получаем несколько изображений postORM.get(j).setUrl_img(response.body().get(i).getGuid().getRendered() + " "); } } j++; } @Override public void onFailure(Call<ArrayList<ImageModel>> call, Throwable t) { } }); } } @Override public void onFailure(Call<ArrayList<PostModel>> call, Throwable t) { } });