【问题标题】:Facebook user albums images pagination using latest facebook SDK?Facebook 用户相册图像分页使用最新的 Facebook SDK?
【发布时间】:2016-04-11 19:59:15
【问题描述】:

我必须在其中获取用户相册和整个图像。到目前为止我做了什么。

第 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();

第 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();

你可以看到我指定了parameters.putString("limit", count); 当请求专辑图片时,count 是专辑中可用的图片。一旦计数超出100,响应没有返回所有数据。在这里我开始知道需要分页之类的东西。我该怎么做用于检索相册中所有可用图像的基于偏移量的分页?谁能帮帮我。

【问题讨论】:

标签: android facebook facebook-graph-api pagination


【解决方案1】:

在 facebook 中获取用户照片的分页有三种类型,即基于光标的分页、基于时间的分页和基于偏移的分页。在您的情况下,您可以按照查询中的要求遵循基于偏移的分页。您可能需要在图形请求中包含属性offsetlimit。因此您可以通过初始化零来启动offset,然后将limit 传递为100。这意味着您将从0 获取记录到100请注意,您可以根据请求获取100 记录。更清楚地说,我正在编写用于获取专辑图片 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();
    }

您必须递归调用该函数以从相册中获取整个图像或在 RecyclerView 滚动中正常更改该函数。

用法

getFacebookImages(mAlbumsId, mAlbumsImagecount, offset, limit);

【讨论】:

    猜你喜欢
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多