android - How to work with pagination URL from Soundcloud API with Retrofit -
i'm using retrofit list of tracks soundcloud-api. request results in callback list of tracks - without pagination works. when add parameter pagination (linked_partitioning=1) request, retrofit returns error. cannot figure out how write callback-type url-string next page , list result.
retrofit call:
map<string, string> querymap = new hashmap<string, string>(); querymap.put("client_id" , my_config.client_id); querymap.put("tag", "loop" ); querymap.put("license", "cc-by-sa" ); querymap.put("limit", "10"); // causing different return type ---> error querymap.put("linked_partitioning", "1" ); scservice = retrofitrestadapter.getservice(); scservice.searchtracks(querymap, callback); // callback function success not reached because of error
interface get-request:
@get("/tracks") public void searchtracks(@querymap map<string, string> filters, callback<list<my_track>> cb);
retrofit callback:
callback<list<my_track>> callbacktracks = new callback<list<my_track>>() { @override public void success(list<my_track> tracks, response response) {...} @override public void failure(retrofiterror error) {...} };
retrofit exception:
com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_array begin_object @ line 1 column 2 path $
edit: result pattern of soundcloud api:
without pagination result array/list of track-elements , looks this:
[{"download_url":null,"key_signature":"","user_favorite":false,...}, {...},{...}]
with pagination result element containing collection plus next_href:
{"collection":[ {"download_url":null,"key_signature":"",...}, {...}, {...} ],"next_href":"https://..."}
how need declare returntype of callback separate collection-array , "next_href" result ?
thanks!
from error provided, can guess, after requested pagination, api returns result in object not array. example, before using pagination, result like
[ { item 1, item 2 }, { item 1, item 2 } ]
and after using pagination, result like
{ page: 1, count: 400, results: [ { item 1, item 2 }, { item 1, item 2 } ] }
so may need change mapping pagination. (since not providing result pattern, think cause of error)
answer edit
currently mapping returning json list of my_track class. need make class mapping , declare list of my_track property. example:
public class mytrackholder { public final string next_href; public final list<my_track> collection; public mytrackholder(final string next_href, final list<my_track> collection) { this.next_href = next_href; this.collection = collection; } }
and interface like
@get("/tracks") public void searchtracks(@querymap map<string, string> filters, callback<mytrackholder> cb);
and call interface method follows
callback<mytrackholder> callbacktracks = new callback<mytrackholder>() { @override public void success(mytrackholder trackholder, response response) {...} @override public void failure(retrofiterror error) {...} };
you retrieve tracks using
list<my_track> tracks = trackholder.collection;
Comments
Post a Comment