JSON Response Using Retrofit on Android -
i'm able json response using okhttp3, , want use retrofit parse response name , image it. looked retrofit website , tutorials, still process not clear.
here okhttp3 code json response:
request request = new request.builder().url(url).build(); client.newcall(request).enqueue(new callback() { @override public void onfailure(call call, ioexception e) { e.printstacktrace(); } @override public void onresponse(call call, response response) throws ioexception { if (!response.issuccessful()) throw new ioexception("unexpected code " + response); headers responseheaders = response.headers(); (int = 0, size = responseheaders.size(); < size; i++) { system.out.println(responseheaders.name(i) + responseheaders.value(i)); } system.out.println(response.body().string()); string jdata = response.body().string();// want parse jdata using retrofit } });
the json response looks this:
i want name, id, , image of each artist, appreciated.
update
i added pojo classes 1 of them item class:
public class item { @serializedname("external_urls") @expose private externalurls externalurls; @serializedname("followers") @expose private followers followers; @serializedname("genres") @expose private list<object> genres = new arraylist<object>(); @serializedname("href") @expose private string href; @serializedname("id") @expose private string id; @serializedname("images") @expose private list<object> images = new arraylist<object>(); @serializedname("name") @expose private string name; @serializedname("popularity") @expose private integer popularity; @serializedname("type") @expose private string type; @serializedname("uri") @expose private string uri; /** * * @return * externalurls */ public externalurls getexternalurls() { return externalurls; } /** * * @param externalurls * external_urls */ public void setexternalurls(externalurls externalurls) { this.externalurls = externalurls; } /** * * @return * followers */ public followers getfollowers() { return followers; } /** * * @param followers * followers */ public void setfollowers(followers followers) { this.followers = followers; } /** * * @return * genres */ public list<object> getgenres() { return genres; } /** * * @param genres * genres */ public void setgenres(list<object> genres) { this.genres = genres; } /** * * @return * href */ public string gethref() { return href; } /** * * @param href * href */ public void sethref(string href) { this.href = href; } /** * * @return * id */ public string getid() { return id; } /** * * @param id * id */ public void setid(string id) { this.id = id; } /** * * @return * images */ public list<object> getimages() { return images; } /** * * @param images * images */ public void setimages(list<object> images) { this.images = images; } /** * * @return * name */ public string getname() { return name; } /** * * @param name * name */ public void setname(string name) { this.name = name; } /** * * @return * popularity */ public integer getpopularity() { return popularity; } /** * * @param popularity * popularity */ public void setpopularity(integer popularity) { this.popularity = popularity; } /** * * @return * type */ public string gettype() { return type; } /** * * @param type * type */ public void settype(string type) { this.type = type; } /** * * @return * uri */ public string geturi() { return uri; } /** * * @param uri * uri */ public void seturi(string uri) { this.uri = uri; }
}
here how i'm using retrofit in activity:
private void loadjson() { retrofit retrofit = new retrofit.builder() .baseurl("https://api.spotify.com") .addconverterfactory(gsonconverterfactory.create()) .build(); final artists_interface request = retrofit.create(artists_interface.class); call<item> call = request.getartists(); call.enqueue(new callback<item>() { @override public void onresponse(call<item> call, response<item> response) { if(response.issuccessful()){ item artist = response.body(); system.out.println("the name::::. : " + artist.getname()); } else{ system.out.println(" :::. noo response .::: " ); } } @override public void onfailure(call<item> call, throwable t) { system.out.println("onfail::: " + t); } });
and here how retrofit interface like:
public interface artists_interface { @get("/v1/search?q=beyonce&type=artist") call<item> getartists();
}
i artist.getname() equals null. need name, id, , images inside "items" in json body , pass them listview or recyclerview adapter
this wrong,
headers responseheaders = response.headers(); (int = 0, size = responseheaders.size(); < size; i++) { system.out.println(responseheaders.name(i) + responseheaders.value(i)); }
you trying find name, id, etc. data in headers, while the information in body of response.
just create pojo data model , retrieve information model response object. can use getters , setters access required data.
update
you can create pojo using site, http://www.jsonschema2pojo.org. copy , paste json , generate pojos instantly.
now once have pojo, can either manually parse or use gson or jackson library easily.
update 2
the error quite self-explanatory here,
expected begin_array begin_object @ line 1
it states parser expected array found object in actual json.
see the actual json, starts object , not array, why using list here,
call<list<item>> getartists();
if @ json can see starts object, inside have object key "artists" , have list of items key "items".
you didn't need manually. www.jsonschema2pojo.org have generated you, had include them.
i don't understand why have included list instead of object.
update 3
getters man, simple getters.
suppose have this, suppose.
class data { artists artists; artists getartists() { return artists; } class artists { list<item> list; list<item> getitemlist(){ return list; } } class item { // have item class here } }
now this,
call<data> getartists();
and items,
data.getartists().getitemlist();
is clear?