改造响应会提供错误的数据

时间:2016-04-22 08:15:30

标签: android retrofit android-recyclerview retrofit2

我尝试从vid.me API中获取视频网址,标题和数量。我有GET响应和结果你可以看到 https://api.vid.me/videos/featured 我为recyclerview构建模型,API和自定义适配器。在RecyclerView中我想看视频,但不是播放视频,只是像这样的快照。但是当我启动我的应用程序时,我看到VideoView上的黑色图形。这个数字有区别大小,但它的黑色数字不像视频和标题和分数的字段是空的。 我必须做些什么才能将视频视频更改为普通视频并将数据放入两个文本视图中?

Video class(POJO):
public class Video {

    @SerializedName("clip_url")
    @Expose
    private String clip_url;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("likes_count")
    @Expose
    private Integer likes_count;

    /**
     *
     * @return
     * The url
     */
    public String getClipUrl() {
        return clip_url;
    }

    /**
     *
     * @param clip_url
     * The url
     */
    public void setClip_url(String clip_url) {
        this.clip_url = clip_url;
    }

    /**
     *
     * @return
     * The title
     */
    public String getTitle() {
        return title;
    }

    /**
     *
     * @param title
     * The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     *
     * @return
     * The description
     */
    public String getDescription() {
        return description;
    }

    /**
     *
     * @param description
     * The description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     *
     * @return
     * The score
     */
    public Integer getScore() {
        return likes_count;
    }

    /**
     *
     * @param likes_count
     * The score
     */
    public void setScore(Integer likes_count) {
        this.likes_count = likes_count;
    }

}

RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.VideoViewHolder> {
    List<Video> call;
    RecyclerViewAdapter(List<Video> call){
        this.call = call;

    }
    @Override
    public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row,parent,false);
        VideoViewHolder videoViewHolder = new VideoViewHolder(v);
        return videoViewHolder;
    }

    @Override
    public void onBindViewHolder(VideoViewHolder holder, int position) {
        String video_url = call.get(position).getClipUrl();
holder.videoView.setVideoURI(Uri.parse(video_url));

        holder.video_name.setText(call.get(position).getTitle());
      //  holder.video_like.setText(call.get(position).getScore());
    }

    @Override
    public int getItemCount() {
        return call.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        VideoView videoView;
        TextView video_name;
        TextView video_like;
        public VideoViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
            videoView = (VideoView)itemView.findViewById(R.id.videoview);
            video_name = (TextView) itemView.findViewById(R.id.Videoname_textView);
            video_like = (TextView) itemView.findViewById(R.id.like_textview);
        }
    }
}

FeaturedFragment:

public class FeaturedFragment extends Fragment {
    RecyclerViewAdapter recyclerViewAdapter;
    public static final String ROOT_URL = "https://api.vid.me/";
    public List<Video> videos;
    RecyclerView recList;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_featured, container, false);
        recList = (RecyclerView) rootView.findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        try {
            getVideos();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void getVideos() throws IOException {
        Retrofit retrofitAdapter = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ROOT_URL)
                .build();
        final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);
        Call<Videos> call = videoApi.getFeaturedVideo();
        call.enqueue(new Callback<Videos>() {
            @Override
            public void onResponse(Call<Videos> call, Response<Videos> response) {
                Log.d("MainActivity", "Status Code = " + response.code());
                videos = response.body().videos;
                recyclerViewAdapter = new RecyclerViewAdapter(videos);


                recList.setAdapter(recyclerViewAdapter);
            }

            @Override
            public void onFailure(Call<Videos> call, Throwable t) {

            }


        });
    }

}

1 个答案:

答案 0 :(得分:-2)

请按照本教程学习如何从服务器中检索数据。确保您了解所有代码。如果您还有其他问题,请来这里。

http://www.vogella.com/tutorials/Retrofit/article.html