gpt4 book ai didi

android - 如何使用 Facebook Graph Api 基于游标的分页

转载 作者:可可西里 更新时间:2023-11-01 19:11:30 25 4
gpt4 key购买 nike

我没有找到关于这个主题的任何帮助。文档说

Cursor-based pagination is the most efficient method of paging and should always be used where possible - a cursor refers to a random string of characters which mark a specific item in a list of data. Unless this item is deleted, the cursor will always point to the same part of the list, but it will be invalidated if an item is removed. Therefore, your app shouldn't store any older cursors or assume that they will still be valid.

When reading an edge that supports cursor pagination, you will see the following JSON response:

{
"data": [
... Endpoint data is here
],
"paging": {
"cursors": {
"after": "MTAxNTExOTQ1MjAwNzI5NDE=",
"before": "NDMyNzQyODI3OTQw"
},
"previous": "https://graph.facebook.com/me/albums?limit=25&before=NDMyNzQyODI3OTQw"
"next": "https://graph.facebook.com/me/albums?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
}
}

我正在使用这种格式进行 api 调用,如何循环遍历所有页面

/* make the API call */
new GraphRequest(
session,
"/{user-id}/statuses",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();

最佳答案

我想出了一个使用光标分页遍历 facebook graph api 页面的好方法

    final String[] afterString = {""};  // will contain the next page cursor
final Boolean[] noData = {false}; // stop when there is no after cursor
do {
Bundle params = new Bundle();
params.putString("after", afterString[0]);
new GraphRequest(
accessToken,
personId + "/likes",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
JSONObject jsonObject = graphResponse.getJSONObject();
try {
JSONArray jsonArray = jsonObject.getJSONArray("data");

// your code


if(!jsonObject.isNull("paging")) {
JSONObject paging = jsonObject.getJSONObject("paging");
JSONObject cursors = paging.getJSONObject("cursors");
if (!cursors.isNull("after"))
afterString[0] = cursors.getString("after");
else
noData[0] = true;
}
else
noData[0] = true;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAndWait();
}
while(!noData[0] == true);

关于android - 如何使用 Facebook Graph Api 基于游标的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com