3

I've very basic retrofit implementation:

public interface MoviesApi {
    @GET(Constants.POPULAR_MOVIES_URL)
    Call<AllMovies> getAllMovies(
            @Query("api_key") String apiKey
    );
}

ViewModel:

Call<AllMovies> call = RetrofitService.getAllMovies().getAllMovies(Constants.API_KEY);

        call.enqueue(new Callback<AllMovies>() {
            @Override
            public void onResponse(Call<AllMovies> call, Response<AllMovies> response) {
                staus.setValue(Status.SUCCESS);
                Log.i(TAG, "onResponse: " + response.body());
                _allMovies.setValue(response.body());
            }

            @Override
            public void onFailure(Call<AllMovies> call, Throwable t) {
                Log.i(TAG, "onResponse: " + t.getMessage());

            }
        });

And I'm using it inside my recyclerView:

  String currentUrl = "https://images-na.ssl-images-amazon.com/images/I/518jo3Xlf8L._AC_.jpg";

        holder.title_text.setText(movieItem.getTitle());

        Glide.with(holder.itemView.getContext())
                .load(currentUrl)
                .apply(new RequestOptions().override(400, 600))
                .into(holder.imageView);
    }

Nothing speical here.

My response contain this field:

  "poster_path": "/e1mjopzAS2KNsvpbpahQ1a6SkSn.jpg"

that I should fetch from here:

https://image.tmdb.org/t/p

My question is - where should I fetch this image?

Should I create new image field in my movie model, and immediately after I call getAllMovies I should call getPoster for example and then insert the data to the image field I created?

Or just fetch directly from glide is enough?

It's worth to mention that I need to pass this image to other screen, when I click on recyclerView item for example

Should I create 2 retrofit instances for 2 base url(image and movies has different base url)?

Reference for tmdb image docs: https://developers.themoviedb.org/3/getting-started/images

Michael
  • 583
  • 4
  • 13
  • This is a synchronization issue - You're trying to fetch the image for the recyclerview before it is displayed. By your own description, you aren't sure where to query the result of the image. One useful thing to keep in mind when approaching this is the activity lifecycle in Android https://www.geeksforgeeks.org/activity-lifecycle-in-android-with-demo-app/. Anyways, the issue of downloading an image into a view in android is already solved: https://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – A. Abramov May 26 '20 at 15:20
  • Why not make the `poster_path` part of the movies response, so that you query all data once in one call to `getAllMovies()`? – Hasan Bou Taam May 27 '20 at 09:37

0 Answers0