1
2
Im new in Retrofit,try to get data from one web server,create Model,Interface but this still not working.Problem(maybe) in method onResponse() I add to that method Log.d and Toast but I dont see Log and Toast when launch my app.Why that dont work? I can understand when I get wrong response or something else,but onResponse() dont work in general,how I think.Maybe Toast cant work withoud data,but Log.d must work without it,and Log.d havent data,just code of response. I added all depencies and tryind do this like in all tutorial,what wrong I did and what I can do to fix that? And also I try tu put this data to adapter,but when launch app,I have error in Log "RecyclerView: No adapter attached; skipping layout" maybe it's the same problem.onResponse dont work and adapter doesn't create,because adapter inilialze in onResponse method and if onResponse doesn't work,setadapter to recyclerview doesn't work to.And VideoApi class:
public interface VideoApi {
@GET("/videos/featured")
Call<List<Video>>getFeaturedVideo();
}
Video class:
public class Video {
@SerializedName("url")
@Expose
private String url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("score")
@Expose
private Integer score;
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = 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 score;
}
/**
*
* @param score
* The score
*/
public void setScore(Integer score) {
this.score = score;
}
}
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<List<Video>> call = videoApi.getFeaturedVideo();
call.enqueue(new Callback<List<Video>>() {
@Override
public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
Log.d("MainActivity", "Status Code = " + response.code());
videos.addAll(response.body());
recyclerViewAdapter = new RecyclerViewAdapter(videos);
String result = response.body().get(0).getTitle();
Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
recList.setAdapter(recyclerViewAdapter);
}
@Override
public void onFailure(Call<List<Video>> call, Throwable t) {
}
});
}
}
- Did you declared INTERNET permission in your Manifest file ? – Shadab Ansari Apr 21 '16 at 19:26
- of course I did it.Puted <uses-permission android:name="android.permission.INTERNET"/> in manifest. – rost_proz Apr 21 '16 at 19:43
3
Your json response returns array of
Video
objects. Change List<Video>
everywhere in your Call
object to Videos
where Videos
class is be defined as -public class Videos {
List<Video> videos;
}
Change like this -
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) {
}
});
}
Also, change -
@GET("/videos/featured")
Call<Videos>getFeaturedVideo();