图像未在 android studio 中与 Picasso 一起显示

callumg46

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

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



    }
高拉夫斯加格

适配器类:

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

URL 创建者类:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章