gpt4 book ai didi

android - 改造响应给出错误数据

转载 作者:太空狗 更新时间:2023-10-29 15:28:09 24 4
gpt4 key购买 nike

我正在尝试从 vid.me API 获取视频 url、标题和点赞数。我已获得响应,您可以从中看到结果 https://api.vid.me/videos/featured我为 recyclerview 构建模型、API 和自定义适配器。在 RecyclerView 中我想看视频,但不播放视频,只是快照或类似的东西。但是当我启动我的应用程序时,我在 VideoView 地方看到黑色数字。这个数字有差异大小,但它的黑色图形不像视频,标题和分数字段为空。我必须做什么才能将视频 View 更改为普通视频并将数据放入两个 TextView ?

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

}

回收 View 适配器:

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

精选 fragment :

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) {

}


});
}

}

最佳答案

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

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

关于android - 改造响应给出错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com