1

I am actually consuming Triposo's API to show the list of countries. https://www.triposo.com/api/

However, in responses, there is no numeric Id and I am struggling while incrementing page size. What should I perform on such responses to overcome? For instance, Github API is fully suitable for Paging Library since it returns itemId as numeric.

https://api.github.com/users

Example Response

{ "results": [ { "name": "Benin", "country_id": "Benin", "snippet": "A safe and relatively easy country for travellers to visit; birthplace of the Voodoo religion and former home of the Kingdom of Dahomey.", "parent_id": null, "score": 3.98127216481287, "id": "Benin" } ], "estimated_total": 30845, "more": true } 

ItemKeyedDataSource.java

public class ItemKeyedCountryDataSource extends ItemKeyedDataSource<Integer, CountryResult> { private EndpointHelper endpointHelper; ItemKeyedCountryDataSource() { endpointHelper = EndpointHelper.getInstance(); } @Override public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<CountryResult> callback) { final List<CountryResult> countryResultList = new ArrayList<>(); endpointHelper.getCountryList(0) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .retry(1) .onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() { @Override public Observable<? extends CountryWrapper> call(Throwable throwable) { return Observable.error(throwable); } }) .subscribe(new Subscriber<CountryWrapper>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(CountryWrapper countryWrapper) { if (countryWrapper.getResults().size() > 0) { countryResultList.addAll(countryWrapper.getResults()); callback.onResult(countryResultList); } } }); } @Override public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull final LoadCallback<CountryResult> callback) { final List<CountryResult> countryResultList = new ArrayList<>(); endpointHelper.getCountryList(params.requestedLoadSize) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .retry(1) .onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() { @Override public Observable<? extends CountryWrapper> call(Throwable throwable) { return Observable.error(throwable); } }) .subscribe(new Subscriber<CountryWrapper>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(CountryWrapper countryWrapper) { if (countryWrapper.getResults().size() > 0) { countryResultList.addAll(countryWrapper.getResults()); callback.onResult(countryResultList); } } }); } @Override public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<CountryResult> callback) { //Do nothing } @NonNull @Override public Integer getKey(@NonNull CountryResult item) { return ?; // what do I return here? } } 

ViewModel.java

public class SearchableActivityViewModel extends ViewModel { private LiveData<PagedList<CountryResult>> countryResult; SearchableActivityViewModel() { Executor executor = Executors.newFixedThreadPool(5); CountryResultDataSourceFactory countryResultDataSourceFactory = new CountryResultDataSourceFactory(); PagedList.Config config = new PagedList.Config.Builder() .setEnablePlaceholders(false) .setInitialLoadSizeHint(20) //first load .setPageSize(21) .build(); //noinspection unchecked countryResult = new LivePagedListBuilder<String, CountryResult>(countryResultDataSourceFactory, config) .setFetchExecutor(executor) .build(); } public LiveData<PagedList<CountryResult>> getCountryResult(){ return countryResult; } @Override protected void onCleared() { super.onCleared(); } } 
4
  • The detailed documentation for that Web service is behind a paywall. What is their URL syntax for requesting pages of data? Commented May 28, 2018 at 21:54
  • Thanks, @CommonsWare the URL for requesting the list of countries: triposo.com/api/20180507/… Here offset parameter specifies the index of the object in the result set to start from. Sorry but I should hide my special keys :) Commented May 28, 2018 at 21:59
  • If offset specifies a position, then this feels like a PositionalDataSource. Commented May 28, 2018 at 22:41
  • I'm also struggling with same problem. In my url, I don't have any pageNo kind of thing. It will return all the data in on hit. Did you find any solution? My question is here: stackoverflow.com/q/51999197/5716010 @nuhkoca@CommonsWare Commented Aug 24, 2018 at 10:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.