2012-12-08 40 views
4

我有一个Facebook图形API的问题,没有返回任何请求获取用户的通知。我通过批量请求来完成它,使用相同的语法来获取用户的新闻提要,并且新闻提要请求正常工作。我也启用了“manage_notifications”权限。当我尝试将图形调用的结果解析为通知时,我总是在代码中得到空指针异常。Android Facebook SDK 3.0阅读通知

请注意,当我在我的互联网浏览器中的图形浏览器中输入“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,这就是为什么它不能解析任何东西。看起来图表请求会出现问题,但我无法弄清楚它是什么。就像我说的,获取用户新闻提要(“我/家”)完全相同的方法非常好。

+0

为什么不只是使用getGraphObject()? – Pavlos

+0

我摆脱了.getInnerJSONObject(),它并没有再给我一个错误。让我试着看看我能否从中得到“数据”数组。 – Wenger

+0

我更新了代码,发现GraphObject对象返回null。我会更新帖子。 – Wenger

回答

6

不是传递参数在图形的路径,将其添加为参数要求:

我/通知include_read =真

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); 
+0

感谢您的回复。我使用FQL工作,所以当我重新开始处理通知时,我会试着放弃这一点。 FWIW我使用“me/notifications?include_read = true”作为我的图形路径。通过一个包添加“include_read = true”条件是否做了一些不同于在图形路径中明确声明的东西? – Wenger

+1

我已经看到反馈,它可能实际上在图形路径中添加时被删除。 –

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..."); 
    } 
}