如何在 android 中解析下一页 Facebook (SDK 4.0) Graph 响应?
How to parse next page of Facebook (SDK 4.0) Graph response in android?
我正在获取使用我的 android 应用程序的好友列表,并在列表视图中显示他们。我们从电话中得到的回应:
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 中结果的下一页,因为它是下一页的 link?下一页 api 调用将通过图表 api 还是仅通过 Facebook 完成?
如@CBroe 所述,您使用 getRequestForPagedResults method. As for an example, check the Scrumptious 示例项目。
我扩展了 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;
}
不漂亮,但你明白了。
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();
我正在获取使用我的 android 应用程序的好友列表,并在列表视图中显示他们。我们从电话中得到的回应:
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 中结果的下一页,因为它是下一页的 link?下一页 api 调用将通过图表 api 还是仅通过 Facebook 完成?
如@CBroe 所述,您使用 getRequestForPagedResults method. As for an example, check the Scrumptious 示例项目。
我扩展了 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;
}
不漂亮,但你明白了。
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();