android - Retrofit,onResponse method doesnt work -
im new in retrofit,try data 1 web server,create model,interface still not working.problem(maybe) in method onresponse() add method log.d , toast dont see log , toast when launch app.why dont work? can understand when wrong response or else,but onresponse() dont work in general,how think.maybe toast cant work withoud data,but log.d must work without it,and log.d havent data,just code of response. added depencies , tryind in tutorial,what wrong did , can fix that? , try tu put data adapter,but when launch app,i have error in log "recyclerview: no adapter attached; skipping layout" maybe it's same problem.onresponse dont work , adapter doesn't create,because adapter inilialze in onresponse method , if onresponse doesn't work,setadapter 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 * url */ public string geturl() { return url; } /** * * @param url * url */ public void seturl(string url) { this.url = url; } /** * * @return * title */ public string gettitle() { return title; } /** * * @param title * title */ public void settitle(string title) { this.title = title; } /** * * @return * description */ public string getdescription() { return description; } /** * * @param description * description */ public void setdescription(string description) { this.description = description; } /** * * @return * score */ public integer getscore() { return score; } /** * * @param score * 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) { } }); } }
your json response returns array of video
objects. change list<video>
everywhere in call
object videos
videos
class defined -
public class videos { list<video> videos; }
change -
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();
Comments
Post a Comment