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
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