使用picasso从json数据中获取图像时的null值

时间:2017-10-31 18:42:22

标签: java android json

我试图创建一个电影应用,所以我创建了recycleview以使用api json数据网络在gridlayout中显示海报 现在该应用程序获取电影的名称和它的受欢迎程度没有问题,但无法获取海报,当我调试应用程序时,我发现海报null的值,当涉及到onbindviewholder为什么?

这是recycleview的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

    ArrayList<MovieItem> mMoviesItems;
    private Context context;

    public RecyclerViewAdapter() {

    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public final TextView MoviePopularity;
        public final ImageView MoviePoster;
        public final TextView MovieName;

        public RecyclerViewHolder(View view) {
            super(view);
            MoviePopularity = (TextView)itemView.findViewById(R.id.movie_popularity);
            MoviePoster = (ImageView)itemView.findViewById(R.id.iv_item_movie);
            MovieName = (TextView)itemView.findViewById(R.id.movie_name);
        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        int layoutIdForListItem = R.layout.movie_list_item;
        LayoutInflater inflater = LayoutInflater.from(context);
        boolean shouldAttachToParentImmediately = false;

        View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
        return new RecyclerViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.MoviePopularity.setText(String.valueOf(mMoviesItems.get(position).getPopularity()));
        Picasso.with(this.context).load(mMoviesItems.get(position).getPhoto()).into(holder.MoviePoster);
        holder.MovieName.setText(String.valueOf(mMoviesItems.get(position).getName()));
    }


    @Override
    public int getItemCount() {
        if (null == mMoviesItems)
            return 0;
        else {
            return mMoviesItems.size();
        }
    }

    public void setMovieData(ArrayList<MovieItem> movieData) {
        mMoviesItems = movieData;
        notifyDataSetChanged();
    }

}

这是自定义类:

public class MovieItem {
    private double popularity;
    private String photo;
    private String name;

    public MovieItem(double popularity, String poster_path, String original_title) {
        this.popularity = popularity;
        this.photo = poster_path;
        this.name = original_title;
    }

    public double getPopularity() { return popularity; }
    public String getPhoto() { return photo; }
    public String getName() { return name; }
}

这是json类:     公共决赛班OpenMovieJsonUtils {

    public static ArrayList<MovieItem> getSimpleMovieStringsFromJson(Context context, String moviesJsonString)
            throws JSONException {

        final String RESULTS = "results";
        final String POPULARITY = "popularity";
        final String POSTER_PATH = "poster_path";
        final String ORIGINAL_TITLE = "original_title";

        ArrayList<MovieItem> parsedMovieData = new ArrayList<MovieItem>();

        JSONObject moviesObject = new JSONObject(moviesJsonString);
        JSONArray moviesArray = moviesObject.getJSONArray(RESULTS);

        for (int i = 0; i < moviesArray.length(); i++) {
            double popularity;
            String poster_path;
            String original_title;

            moviesObject = moviesArray.getJSONObject(i);

            popularity = moviesObject.getDouble(POPULARITY);
            poster_path = moviesObject.getString(POSTER_PATH);
            original_title = moviesObject.getString(ORIGINAL_TITLE);

            parsedMovieData.add(new MovieItem(popularity, poster_path, original_title));

        }

        return parsedMovieData;
    }
}

这是网络课程:

public final class NetworkUtils {

    private static final String TAG = NetworkUtils.class.getSimpleName();

    private static final String MOVIES_BASE_URL = "https://api.themoviedb.org/3/movie/";
    private static final String MOVIES_URL  = MOVIES_BASE_URL;
    private static final String apiKey = "36666cbb5d7e20041e705d1b2c4e7a79";

    final static String API_PARAM = "api_key";

    public static URL buildUrl(String MoviesQuery) {
        Uri builtUri = Uri.parse(MOVIES_URL).buildUpon()
                .appendPath(MoviesQuery)
                .appendQueryParameter(API_PARAM, apiKey)
                .build();

        URL url = null;
        try {
            url = new URL(builtUri.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        Log.v(TAG, "Built URI " + url);
        return url;
    }

    public static String getResponseFromHttpUrl(URL url) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream in = urlConnection.getInputStream();

            Scanner scanner = new Scanner(in);
            scanner.useDelimiter("\\A");

            boolean hasInput = scanner.hasNext();
            if (hasInput) {
                return scanner.next();
            } else {
                return null;
            }
        } finally {
            urlConnection.disconnect();
        }
    }
}

图像有什么问题,所以我的值为null,而不是电影的受欢迎程度和名称,而不是null

1 个答案:

答案 0 :(得分:2)

您的问题不完整,没有详细说明您获取API响应的详细信息。

我可以回答这个问题,因为我刚刚在Udacity正在进行的Android Developer Nanodegree中完成了这个项目。

希望你从TMDB获取电影细节。

您应该仔细阅读项目实施指南

  

您需要在此相对路径之前附加基本路径,以构建使用Picasso获取图像所需的完整网址

以下代码可以帮助您 -

public class MovieItem {
    private double popularity;
    private String photo;
    private String name;

    public MovieItem(double popularity, String poster_path, String original_title) {
        this.popularity = popularity;
        this.photo = poster_path;
        this.name = original_title;
    }

    public double getPopularity() { return popularity; }
    public String getPhoto() { return photo; }
    public String getName() { return name; }

    public Uri getFullPosterPath() {
        return Uri.parse("http://image.tmdb.org/t/p/")
                .buildUpon()
                .appendPath("w185")
                .appendEncodedPath(getPhoto())
                .build();
    }
}

在适配器中进行此更改 -

@Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.MoviePopularity.setText(String.valueOf(mMoviesItems.get(position).getPopularity()));
        Picasso.with(this.context).load(mMoviesItems.get(position).getFullPosterPath()).into(holder.MoviePoster);
        holder.MovieName.setText(String.valueOf(mMoviesItems.get(position).getName()));
    }