Last.fm java API - how to get users from PaginatedResult

余生颓废 提交于 2019-12-24 11:36:49

问题


I am a but puzzled about the lastfm API, more exactly about the PaginatedResult

I read all users from a group in a PaginatedResult:

PaginatedResult<de.umass.lastfm.User> users = Group.getMembers("Classic Rock", key);

Then I tried two methods to display all the users. There should be about 25000, but I only get about 25. Only first page? How could I get all the results?

//        for (int i = 0; i < users.getTotalPages();i++){
            for (User thisuser: users.getPageResults()){
                //for each user
                System.out.print(thisuser.getName() + " - age: " + thisuser.getAge() + " - country: " + thisuser.getCountry() + " - "+ thisuser.getGender() + " - is: " + thisuser.getId() + " - playcount: " + thisuser.getPlaycount() + " - num playlists: " + thisuser.getNumPlaylists() + "\n");
            }
//        }

        for (User thisuser: users){
            //for each user
            System.out.print(thisuser.getName() + " - age: " + thisuser.getAge() + " - country: " + thisuser.getCountry() + " - "+ thisuser.getGender() + " - is: " + thisuser.getId() + " - playcount: " + thisuser.getPlaycount() + " - num playlists: " + thisuser.getNumPlaylists() + "\n");
        }

回答1:


The getMembers(String group, String apiKey) method you're using only gets the first page in the paginated result. You need to use the getMembers(String group, int page, String apiKey) overload and call it for each page you want to retrieve beyond the first page. Each result contains information about how many pages are available.

And don't forget the Last.FM API usage rules about how often you can make API calls - the 1000 calls needed to get the 25000 Classic Rockers would take 1½ hours to retrieve (5 seconds between requests).

From the documentation;

A PaginatedResult is returned by methods which result set might be so large that it needs to be paginated. Each PaginatedResult contains the total number of result pages, the current page and a Collection of entries for the current page.



来源:https://stackoverflow.com/questions/22617045/last-fm-java-api-how-to-get-users-from-paginatedresult

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!