我正在获取使用我的android应用程序的朋友列表,并在listview中显示它们。我们从调用中得到的响应:
GraphRequestAsyncTask graphRequest = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
}
}
).executeAsync();
{
"data": [
{
"name": "Sanjeev Sharma",
"id": "10XXXXXXXXXX40"
},
{
"name": "Avninder Singh",
"id": "1XXXXX30"
},
{
"name": "Saikrishna Tipparapu",
"id": "17XXXXXX98"
},
{
"name": "Perfekt Archer",
"id": "100XXXXX29"
},
{
"name": "Shathyan Raja",
"id": "10XXXXX0"
},
{
"name": "Kenny Tran",
"id": "10XXXXX36164"
},
{
"name": "Lahaul Seth",
"id": "100XXXXX161"
},
{
"name": "Bappa Dittya",
"id": "10XXXXX24"
},
{
"name": "Rahul",
"id": "10XXXXX
},
{
"name": "Suruchi ",
"id": "7XXXXXXXX11"
}
],
"paging": {
"next": "https://graph.facebook.com/76XXXXXXXX28/friends?limit=25&offset=25&__after_id=enc_AdAXXXXX5L8nqEymMrXXXXoYWaK8BXXHrvpXp03gc1eAaVaj7Q"
},
"summary": {
"total_count": 382
}
}
现在我们如何解析下一页的结果在android,因为它是下一页的链接?下一页api调用将通过图形api或facebook只做?
ifaour有如何使用分页与下一个正确的想法,虽然我认为他是对的,我只是想添加一个递归的方式来获取所有的结果页后页到一个漂亮的列表对象,这是来自一个请求用户照片的项目,但它的思想和语法与喜欢相同(注意,这整个事情是使用执行和等待,所以你必须在一个单独的线程中运行它,否则你将有效地阻塞你的UI线程,最终使应用程序关闭自己。
Bundle param = new Bundle();
param.putString("fields", "id,picture");
param.putInt("limit", 100);
//setup a general callback for each graph request sent, this callback will launch the next request if exists.
final GraphRequest.Callback graphCallback = new GraphRequest.Callback(){
@Override
public void onCompleted(GraphResponse response) {
try {
JSONArray rawPhotosData = response.getJSONObject().getJSONArray("data");
for(int j=0; j<rawPhotosData.length();j++){
/*save whatever data you want from the result
JSONObject photo = new JSONObject();
photo.put("id", ((JSONObject)rawPhotosData.get(j)).get("id"));
photo.put("icon", ((JSONObject)rawPhotosData.get(j)).get("picture"));
boolean isUnique = true;
for(JSONObject item : photos){
if(item.toString().equals(photo.toString())){
isUnique = false;
break;
}
}
if(isUnique) photos.add(photo);*/
}
//get next batch of results of exists
GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
if(nextRequest != null){
nextRequest.setCallback(this);
nextRequest.executeAndWait();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
现在你所需要做的就是简单地发出初始请求并设置你在上一步中所做的回调,回调将处理调用其余项目的所有脏工作,这将最终为你提供来自请求的所有项目。
//send first request, the rest should be called by the callback
new GraphRequest(AccessToken.getCurrentAccessToken(),
"me/photos",param, HttpMethod.GET, graphCallback).executeAndWait();
如@CBroe所述,您使用getRequestForPagedResults方法。作为示例,请查看scrumtious示例项目。
我扩展了HelloFacebookSample并添加了两个按钮,它们将加载初始用户喜欢的页面,另一个将加载下一个结果(如果可用):
loadAndLogLikesButton = (Button) findViewById(R.id.loadAndLogLikesButton);
loadAndLogLikesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pendingAction = PendingAction.LOAD_LIKES;
if (!hasUserLikesPermission()) {
LoginManager.getInstance().logInWithReadPermissions(HelloFacebookSampleActivity.this, Arrays.asList("public_profile", "user_likes"));
} else {
handlePendingAction();
}
}
});
现在正在从LoginManager成功回调调用handlePendingAction()
。正如您所看到的,我有一个额外的动作LOAD_LIKES
,它将触发一个方法,该方法将执行以下操作:
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"me/likes",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
Log.d("HelloFacebook", response.getRawResponse());
JSONArray data = response.getJSONObject().optJSONArray("data");
boolean haveData = data.length() > 0;
if (haveData) {
loadNextLikesButton.setEnabled(true);
nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "id");
parameters.putString("limit", "100");
request.setParameters(parameters);
现在我的loadNextLikesButton
的回调看起来像这样:
if (nextRequest != null) {
nextRequest.setCallback(new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
Log.d("HelloFacebook", response.getRawResponse());
JSONArray data = response.getJSONObject().optJSONArray("data");
boolean haveData = data.length() > 0;
if (haveData) {
loadNextLikesButton.setEnabled(true);
nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
} else {
loadNextLikesButton.setEnabled(false);
}
}
});
nextRequest.executeAsync();
} else {
Log.d("HelloFacebook", "We are done!");
return;
}
不好看,但你懂的。