Reputation: 27
I have a JSON response from a marvel api, i can get the copyright info from it that is in the first level, here is the response to the service:
{
"code": 200,
"status": "Ok",
"copyright": "© 2019 MARVEL",
"attributionText": "Data provided by Marvel. © 2019 MARVEL",
"attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2019 MARVEL</a>",
"etag": "a1d8666dc86abda3bd2edf99d09446da82626c4b",
"data": {
"offset": 0,
"limit": 20,
"total": 1,
"count": 1,
"results": [
{
"id": 1009664,
"name": "Thor",
"description": "As the Norse God of thunder and lightning, Thor wields one of the greatest weapons ever made, the enchanted hammer Mjolnir. While others have described Thor as an over-muscled, oafish imbecile, he's quite smart and compassionate. He's self-assured, and he would never, ever stop fighting for a worthwhile cause.",
.
.
.
}
I have my getters and setters like this:
private int id;
private String name;
private String description;
private String thumbnail;
private String copyright;
public Heroe() {}
public Heroe(String copyright, int id, String name, String description, String thumbnail) {
this.copyright = copyright;
this.id = id;
this.name = name;
this.description = description;
this.thumbnail = thumbnail;
}
but i want to get the result from the data. to get the id, name, and description, here is my code:
Android response service from MARVEL
Upvotes: 1
Views: 172
Reputation: 61
First create getter() and setter() methods in model class
Example :-
public class RetroPhoto {
@SerializedName("id")
private Integer id;
@SerializedName("title")
private String title;
@SerializedName("item")
public Item a;
public RetroPhoto(Integer albumId, Integer id, String title, String url, String thumbnailUrl) {
this.albumId = albumId;
this.id = id;
this.title = title;
this.url = url;
this.thumbnailUrl = thumbnailUrl;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Item getItem11() {
return a;
}
public void setItem11(Item item11) {
this.a = item11;
}
public class Item{
@SerializedName("id_item")
private String idItem;
@SerializedName("id_subcategory")
private String idSubcategory;
public String getIdItem() {
return idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public String getIdSubcategory() {
return idSubcategory;
}
public void setIdSubcategory(String idSubcategory) {
this.idSubcategory = idSubcategory;
}
}
}
How to get idSubcategory :-
RetroPhoto.Item ri = response.getItem11();
String idSubcategory = ri.getIdSubcategory();
Upvotes: 0
Reputation: 1627
Since the json response has a child node try to Create a custom response like
HeroResponse.java
class HeroResponse {
private int code;
private String status;
//.. other variables
@SerializedName("data")
private Hero data
//.. Getter and setter
}
Hero.java
class Hero {
private int offset;
private int limit;
//.. other variables
@SerializedName("results")
private HeroDetail results
//.. Getter and setter
}
HeroDetail.java
class HeroDetail {
private int id;
private String name;
private String description
//.. Getter and setter
}
then on your retrofit call
heroeCall.enqueue(
//..
public void onResponse(Call<HeroResponse> call, Response<HeroResponse> response) {
HeroResponse resp = response.body();
resp.copyright
resp.data.offset
resp.data.results.id
}
)
Also don't forget to implement the 'com.google.code.gson:gson:2.8.5'
in your app build.gradle if you want to use the annotation SerializedName
Hope this will help.
Upvotes: 1