Search code examples
javaandroidpicasso

Images not displaying with Picasso in android studio


Using Picasso to display multiple images taken from a web API, however only one image is displaying out of 20. Is this a memory or cache issue? I have used the same code elsewhere in the app and it works just fine. I'm sure this could be a loop too but as I said the code has worked elsewhere in the app.

 protected void onPostExecute(String nowplaying) {

        if (nowplaying == null) {
            nowplaying = "THERE WAS AN ERROR";
        }

        try {


            JSONObject object = (JSONObject) new JSONTokener(nowplaying).nextValue();
            JSONArray all_results = object.getJSONArray("results");

            String ImageURL = "https://image.tmdb.org/t/p/w500";


            ImageURL += (all_results.getJSONObject(0).getString("poster_path"));
            TestName1.append(all_results.getJSONObject(0).getString("title"));

            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster1);

            ImageURL += (all_results.getJSONObject(1).getString("poster_path"));
            TestName2.append(all_results.getJSONObject(1).getString("title"));

            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster2);

            ImageURL += (all_results.getJSONObject(2).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster3);

            ImageURL += (all_results.getJSONObject(3).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster4);

            ImageURL += (all_results.getJSONObject(4).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster5);

            ImageURL += (all_results.getJSONObject(5).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster6);

            ImageURL += (all_results.getJSONObject(6).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster7);

            ImageURL += (all_results.getJSONObject(7).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster8);

            ImageURL += (all_results.getJSONObject(8).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster9);

            ImageURL += (all_results.getJSONObject(9).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster10);

            ImageURL += (all_results.getJSONObject(10).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster11);

            ImageURL += (all_results.getJSONObject(11).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster12);

            ImageURL += (all_results.getJSONObject(12).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster13);

            ImageURL += (all_results.getJSONObject(13).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster14);
            ImageURL += (all_results.getJSONObject(14).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster15);

            ImageURL += (all_results.getJSONObject(15).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster16);

            ImageURL += (all_results.getJSONObject(16).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster17);

            ImageURL += (all_results.getJSONObject(17).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster18);

            ImageURL += (all_results.getJSONObject(18).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster19);

            ImageURL += (all_results.getJSONObject(19).getString("poster_path"));
            Picasso.with(getApplicationContext()).load(ImageURL).into(BookTicketsFilmPoster20); 



        } catch (JSONException e) {
            e.printStackTrace();
        }



    }

Solution

  • Adapter Class:

    URL url = null;
    url = GenerateMovieThumbnailsURL.buildURL(list.get(count).getMoviePosterPath());     
    Picasso.with(mContext).load(url.toString()).into(viewHolder.mMovieItem);
    

    URL Creator Class:

    public class GenerateMovieThumbnailsURL {
      final static String BASE_MOVIE_THUMBNAIL_URL = "https://image.tmdb.org/t/p/";
    
      final static String imageQuality = "w185";
      String imageURL;
    
      GenerateMovieThumbnailsURL(String imageExtendedURI) {
        imageURL = imageExtendedURI;
      }
    
    public static URL buildURL(String imageLink) {
    
        Uri builtURI = Uri.parse(BASE_MOVIE_THUMBNAIL_URL).buildUpon()
                .appendPath(imageQuality)
                .appendEncodedPath(imageLink)
                .build();
    
    
        URL url = null;
        try {
            url = new URL(builtURI.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    
        return url;
    
      }
    }
    

    Extract Image Thumbnail Path:

    JSONArray jsonArray = jsonObject.getJSONArray("results");
    
    
                    for (int i = 0; i < jsonArray.length(); i++) {
    
                        JSONObject js = jsonArray.getJSONObject(i);
                        MovieItem item = new MovieItem();
                        item.setMovieTitle(js.getString("poster_path"));
                        list.add(item);}