在android工作室中没有与Picasso一起显示的图像

时间:2018-04-15 19:09:25

标签: java android picasso

使用Picasso显示从Web API获取的多个图像,但是只显示一个图像。这是内存还是缓存问题?我在应用程序的其他地方使用了相同的代码,它工作正常。我确定这也可能是一个循环,但正如我所说,代码已经在应用程序的其他地方工作。

 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();
        }



    }

2 个答案:

答案 0 :(得分:0)

适配器类:

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

网址创建者类:

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;

  }
}

提取图像缩略图路径:

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);}

答案 1 :(得分:0)

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

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

ImageURL1 = ImageURL + (all_results.getJSONObject(1).getString("poster_path"));

TestName2.append(all_results.getJSONObject(1).getString("title"));



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