安卓Facebook SDK 3.0读取通知

4
我在使用Facebook graph API 请求用户通知时遇到了问题,没有返回任何内容。我是通过批量请求来实现的,与获取用户新闻动态相同的语法,而且我的“manage_notifications”权限已启用。但是当我尝试解析通知的graph调用结果时,我的代码总是抛出NullPointerException异常。
需要注意的是,当我在Internet浏览器中打开Graph探索器并输入“me/notifications?include_read=true”时,我确实得到了适当的数据。
以下是我发起请求的代码:
static Request notificationsRequest = Request.newGraphPathRequest(fbSession, "me/notifications?include_read=true", new Request.Callback() {

    @Override
    public void onCompleted(Response response) {
        GraphObject object = response.getGraphObject();
        if(object != null){
            notifications = object.getProperty("data").toString();
        }
        else{
            notifications = "Notifications returns null";
        }

    }
});

任何想法吗?谢谢。 编辑: 我更新了代码,发现GraphObject对象返回null,因此无法从中解析任何内容。看起来图形请求可能存在问题,但我无法弄清楚原因。就像我说的,获取用户新闻提要("me/home")的方法完全正常。

为什么不直接使用getGraphObject()呢? - Pavlos
我移除了 .getInnerJSONObject() 的部分,现在没有出现错误了。让我尝试看看是否能从中获取 "data" 数组。 - Wenger
我更新了代码,发现GraphObject对象返回null。我会更新帖子。 - Wenger
尝试使用fql进行查询吧,它可能更有帮助! - Pavlos
那是计划B。我一直试图用图调用来完成所有事情,但我想我会尝试一下FQL。谢谢。 - Wenger
2个回答

6

不要将参数放在图形路径中,而是将其作为请求的参数添加:

me/notifications?include_read=true

static Request notificationsRequest = Request.newGraphPathRequest(fbSession, "me/notifications", new Request.Callback() {

    @Override
    public void onCompleted(Response response) {
        GraphObject object = response.getGraphObject();
        if(object != null){
            notifications = object.getProperty("data").toString();
        }
        else{
            notifications = "Notifications returns null";
        }

    }
});


Bundle params = new Bundle();
params.putString("include_read", "true");

notificationsRequest.setParameters(params);

谢谢回复。我已经使用FQL使其工作,所以当我回到处理通知时,我会尝试这个方法。顺便说一下,我使用的是“me/notifications?include_read=true”作为我的图路径。通过bundle添加“include_read=true”条件是否与在图路径中明确声明有所不同? - Wenger
1
我看到反馈说,当添加到图路径中时,它可能会被删除。 - C Abernathy

0

我找到了另一种方法。如果有人发现它有用,我会发布它。

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        InboxMessage.setVisibility(View.VISIBLE);

        new Request(session,"/me/notifications", null,HttpMethod.GET,new Request.Callback() {

                    public void onCompleted(Response response) {
                        GraphObject object = response.getGraphObject();
                            if (object != null) {
                                InboxMessage.setText(object.getProperty("data").toString());
                            } else {
                                InboxMessage.setText("Notifications returns null");
                            }
                    }
                }
            ).executeAsync();
    } else {
        InboxMessage.setVisibility(View.INVISIBLE);
        Log.i(TAG, "Logged out...");
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接