[英]Facebook user albums images pagination using latest facebook SDK?
到目前为止,我必须获取用户相册和整个图像。
第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
,响应就没有返回所有数据。在这里我知道需要类似分页的方法。检索相册中所有可用的图像? 谁能帮我吗。
在Facebook中获取用户照片的分页类型有三种,即基于游标的分页,基于时间的分页和基于偏移量的分页。在您的情况下,您可以按照查询中的要求遵循基于偏移量的分页。以便在图形请求中包含具有limit
的属性offset
,您可以通过初始化零开始offset
,然后将limit
传递为100
意味着您将获得0
到100
记录。 并请注意,每个请求可以获取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();
}
您必须对函数进行递归调用,以从相册中获取整个图像或正常地在RecyclerView滚动条中更改函数。
用法
getFacebookImages(mAlbumsId, mAlbumsImagecount, offset, limit);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.