[英]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.