java - How to parse Retrofit 2 response to display data? -


this question has answer here:

i want display "title" api(http://jsonplaceholder.typicode.com/posts) using retrofit 2 , gson(or gsonformat, i'm not sure),
i have created following classes,

modal.java

package arpit.retrodemo;  public class modal {     private string title;     public string gettitle(){         return title;     } } 

apiservice interface,

package arpit.retrodemo;  import java.util.list;  import retrofit.call; import retrofit.http.get;  public interface apiservice {     @get("/posts")     call<list<modal>> getdetails(); } 


mainactivity.java

package arpit.retrodemo;  import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log;   import java.util.list;  import retrofit.call; import retrofit.callback; import retrofit.gsonconverterfactory; import retrofit.response; import retrofit.retrofit;  public class mainactivity extends appcompatactivity {      private static final string endpoint = "http://jsonplaceholder.typicode.com";     private apiservice service;     private static list<modal> users;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          retrofit retrofit = new retrofit.builder()                 .addconverterfactory(gsonconverterfactory.create())                 .baseurl(endpoint)                 .build();         service = retrofit.create(apiservice.class);         call<list<modal>> userlist = service.getdetails();         userlist.enqueue(new callback<list<modal>>() {             @override             public void onresponse(response<list<modal>> response) {                 if(response.issuccess()){                     log.d("find", "response.issuccess");                     // parse api reponse(in json) display, say, "title" api response                 }             }              @override             public void onfailure(throwable t) {                 log.d("finderror", t.getmessage());             }         });      } } 


json format

[   {     "userid": 1,     "id": 1,     "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",     "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"   },   {     "userid": 1,     "id": 2,     "title": "qui est esse",     "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"   },   {     "userid": 1,     "id": 3,     "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",     "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"   } ] 


so response api in onresponse method but

how can store each title in object of modal.java class , list "users" contain objects of modal.java can later display titles using iterator on "users" list.

1). first of all, please add setter function title in modal class.

2). can find list of modal class response.body(). example.

   @override         public void onresponse(response<list<modal>> response) {             if(response.issuccess()){                 list<modal> titleslist=response.body();             }         } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -