简体   繁体   English

Facebook用户使用最新的Facebook SDK相册图像分页吗?

[英]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 第1步:获取用户相册详细信息

 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 第2步:使用相册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); 您可以看到我正在指定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 ? 当请求专辑图像时, count是专辑中可用的图像。一旦计数超过100 ,响应就没有返回所有数据。在这里我知道需要类似分页的方法。检索相册中所有可用的图像? 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 . 在Facebook中获取用户照片的分页类型有三种,即基于游标的分页,基于时间的分页和基于偏移量的分页。在您的情况下,您可以按照查询中的要求遵循基于偏移量的分页。以便在图形请求中包含具有limit的属性offset ,您可以通过初始化零开始offset ,然后将limit传递为100意味着您将获得0100记录。 and note that you can fetch 100 records per request .for more clearly I'm writing a code sample for fetching album images URL. 并请注意,每个请求可以获取100条记录 。为了更清楚地说明,我正在编写代码示例以获取相册图片网址。

// 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. 您必须对函数进行递归调用,以从相册中获取整个图像或正常地在RecyclerView滚动条中更改函数。

Usage 用法

getFacebookImages(mAlbumsId, mAlbumsImagecount, offset, limit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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