简体   繁体   中英

Facebook user albums images pagination using latest facebook SDK?

I have to get user albums and entire images within that.what i done so far.

Step 1 : Fetch user albums details

 Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,picture.type(album),count");
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),  //your fb AccessToken
                "/" + AccessToken.getCurrentAccessToken().getUserId() + "/albums",//user id of login user
                parameters,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(final GraphResponse response) {
         }

        }).executeAsync();

Step 2 : Fetch images within the albums using album id

 Bundle parameters = new Bundle();
        parameters.putString("fields", "images");
        parameters.putString("limit", count);
        /* make the API call */
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "/" + albumId + "/photos",
                parameters,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {
                }

        }).executeAsync();

You can see I'm specifying parameters.putString("limit", count); when requesting for albums images and the count is the images available in albums.once the count going beyond 100 the response haven't returning all the data.here I came to know something like pagination is needed.how can i do offset based pagination for retrieving all available images in the albums ? can any one help me out.

There are three type of pagination for fetching user photos in facebook ie, Cursor-based Pagination,Time-based Pagination and Offset-based Pagination.In your case you can follow Offset-based Pagination as your asked in query.for that you might need to include an attribute offset with limit in your graph request.so you can start offset by initializing zero then pass the limit as 100 .which means you will get records from 0 to 100 . and note that you can fetch 100 records per request .for more clearly I'm writing a code sample for fetching album images URL.

// ArrayList for storing images URL
private ArrayList<String> albumImages= new ArrayList<>();
// Records offset value, initially zero
private int offset = 0;
// Records count would like to fetch per request
private int limit = 100;

private void getAlbumsImages(final String albumId, final int count, int offsetValue, int limitValue) {
        offset = offsetValue;
        limit = limitValue;
        Bundle parameters = new Bundle();
        parameters.putString("fields", "images");
        parameters.putString("offset", String.valueOf(offset));
        parameters.putString("limit", String.valueOf(limit));

        /* make the API call */
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "/" + albumId + "/photos",
                parameters,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {
                      /* handle the result */
                        try {
                            if (response.getError() == null) {
                                JSONObject joMain = response.getJSONObject();
                                if (joMain.has("data")) {
                                    JSONArray jaData = joMain.optJSONArray("data");
                                    for (int i = 0; i < jaData.length(); i++)//Get no. of images
                                    {
                                        JSONObject joAlbum = jaData.getJSONObject(i);
                                        JSONArray jaImages = joAlbum.getJSONArray("images");// get images Array in JSONArray format
                                if (jaImages.length() > 0) {
                             albumImages.add(jaImages.getJSONObject(0).getString("source"));
                                        }
                                    }
                                }

                                if (count > offset + limit) {
                                    offset = offset + limit;
                                    if (count - offset >= 100)
                                        limit = 100;
                                    else
                                        limit = count - offset;
                                    getFacebookImages(albumId, count, offset, limit);
                                }
                            } else {
                                Log.e("Error :", response.getError().toString());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }
        ).executeAsync();
    }

You have to make a recursive call to the function to fetch the whole images from the albums or alter the function normally in your RecyclerView scroll.

Usage

getFacebookImages(mAlbumsId, mAlbumsImagecount, offset, limit);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM