i have this json's and i want to use retrofit for 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 that json is in array mode my code work. but my question is how parse nested json like above sample?
here my javaClass: (statusClass)
public class retroStatus {
@SerializedName("status")
private String status;
@SerializedName("data")
private ArrayList<retroPost> data;
//getters And 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 And 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()) {
for (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 the app, onFailure happend but if main json just in onejsonArray and use just retroPost for parsing that thats work's very nice... what i mistake ?
sorry for bad english...
See Question&Answers more detail:
os