rost_proz rost_proz - 7 days ago 6
Android Question

Retrofit response give wrong data

Im trying toget video url,title and number oflike from vid.me API.I have GET respone and result you can see from
https://api.vid.me/videos/featured
I build model,API and custom adapter for recyclerview.In RecyclerView I want to see video,but not playing video,just snapshot orsomething like this.But when I start my app,I see black figure on the VideoView place.This figure have difference size,but its black figure not like video and fields for title and score are empty.
What I must do to change videoview to normal video and put data to both textviews?

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

}


});
}

}

Answer

Please follow this tutorial and learn how to retrive data from server. Make sure you understand all the code. Then come here, if you have any further questions.

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

Comments