从Facebook图形api android获取帖子和喜欢

时间:2015-08-18 21:06:13

标签: android facebook facebook-graph-api facebook-android-sdk facebook-access-token

我正试图从facebook图表api获得帖子和他们的喜欢。当我查询获取所有帖子时,它返回200状态但是null json对象,即使我已设置权限并且查询看起来很好。这是我正在使用的查询

 Log.d(TAG, " jajajaj "+ MainActivity.accessToken);
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/me/posts",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    Log.d(TAG, " FB Data "+ response.toString());
                    try{
                        JSONObject main = new JSONObject();
                        JSONArray jArray = main.getJSONArray("data");
                        Log.d(TAG, " JArray size "+ jArray.length());
                        for(int i = 0; i < jArray.length(); i++){
                            Log.d(TAG, " Real Data "+ jArray.getJSONObject(i).get("id"));
                        }
                    } catch (JSONException exception){
                        exception.printStackTrace();
                    }
                }
            }
    ).executeAsync();

收到response.toString()

 {Response:  responseCode: 200, graphObject: {"data":[]}, error: null}

请求的权限集如下:

LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("user_posts", "public_profile", "user_friends", "user_likes","user_events","read_stream", "user_about_me", "user_friends" ));

我真的不明白我做错了什么......

提前致谢

1 个答案:

答案 0 :(得分:0)

您的代码必须类似于以下代码:

new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/me/posts",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    JSONObject object = response.getJSONObject();
       for (int i = 0; i < object.optJSONArray("data").length(); i++){
Log.d(TAG, " Real Data"+object.optJSONArray("data").optJSONObject(i).optString("id"));

}
}});
request.executeAsync();

也就是说,您创建一个新的JSonObject,它是null而不是使用从Facebook Graph API发送的GraphResponse对象。