android - Parse a nested json with retrofit 2.0 -
i have json's , want use retrofit parsing them.
{ "status": "true", "data": [ { "id": "1", "title": "hi :)", "text": "<p>121212</p>", "cat_id": "1", "username": "admin", "coin": "0", "datetime": "1451508880", "isshow": "1" }, { "id": "3", "title": " hi :)", "text": "hi :)", "cat_id": "2", "username": "hi :)", "coin": "20", "datetime": "1451508880", "isshow": "1" }, { "id": "4", "title": "a", "text": "sometext", "cat_id": "1", "username": "admin", "coin": "10", "datetime": "1451982292", "isshow": "1" } ] } when json in array mode code work. question how parse nested json above sample?
here javaclass: (statusclass)
public class retrostatus { @serializedname("status") private string status; @serializedname("data") private arraylist<retropost> data; //getters , setters } (retropost class)
public class retropost { @serializedname("id") private int id; @serializedname("title") private string title; @serializedname("text") private string text; @serializedname("cat_id") private int cat_id; @serializedname("datetime") private long datetime; //getters , setters } (getpostinterface)
public interface getpost { @get("get/posts/all") call<list<retrostatus>> getpost(); } (and maincode)
retrofit retrofit = new retrofit.builder() .baseurl("http://mywebsite.it/api/") .addconverterfactory(gsonconverterfactory.create()) .build(); getpost postservice = retrofit.create(getpost.class); call<list<retrostatus>> call = postservice.getpost(); callback<list<retrostatus>> callback = new callback<list<retrostatus>>() { @override public void onresponse(response<list<retrostatus>> response, retrofit retrofit) { if (response.issuccess()) { (retrostatus status : response.body()) { log.e("test", "nowstatusis: " + status.getstatus()); } } else { log.e("test", "failed"); } } @override public void onfailure(throwable t) { log.e("test", "onfailure"); } }; call.enqueue(callback); now when run app, onfailure happend if main json in onejsonarray , use retropost parsing thats work's nice... mistake ?
sorry bad english...
root node of json not array, declared list<responsestatus> @ interface getpost. need use responsestatus instead current one. , use gettter access data
@serializedname("data") private arraylist<retropost> data; which array
Comments
Post a Comment