使用Facebook Graph API对象填充列表

时间:2015-08-11 02:06:00

标签: android facebook facebook-graph-api arraylist

我正在尝试使用Graph API中的JSON对象填充String列表,其中包含facebook帖子的full_picture,message和id字段。问题是我的名单似乎根本没有填充。

 String TAG = ImageIngester.class.getSimpleName();
    List<ImagePost> fbImages = new ArrayList<>();
    ImagePost fbImagePost;

    @Override
    public List<ImagePost> ingest() {

//    1. Reach out to Facebook SDK and get all image posts
        Bundle bundle = new Bundle();
        bundle.putString("fields","posts{message,full_picture,object_id}");
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "me",
                bundle,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {
                        //list of strings representing a JSON string
                        List<String> fbPosts = new ArrayList<>();
                        try {
                            JSONObject jsonObject = response.getJSONObject();
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            Log.d(TAG, jsonObject.toString());
                            for (int i = 0; i< jsonArray.length(); i++){
                                fbPosts.add(jsonArray.getString(i));
                                Log.d(TAG, fbPosts.toString());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                            for (int i = 0; i < fbPosts.size(); i++){
                                fbImages.add(i,fbImagePost);
                                Log.d(TAG, "added to ImagePost list");
                            }
                        Log.d(TAG, "post received");
                    }
                }
        ).executeAsync();
        return fbImages;
        }
    }

我希望fbPosts列表开始填充我从GraphResponse获得的对象,但它保持在0大小,我得到的唯一图形响应是我自己的ID。

1 个答案:

答案 0 :(得分:0)

看起来你要求response.data(这会让FB图形调用会返回这样的内容)

{
    "data": {
    ....
    }
}

但在这种情况下使用帖子,当我运行你的查询时

/me?fields=posts{message,full_picture,object_id})

通过developer.facebook.com上的GraphAPIExplorer,它返回以下内容:

{
  "posts": {
    "data": [
      {
        "message": "<MESSAGE WAS HERE>.",
        "full_picture": "https://scontent.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/s720x720/blah.jpg?oh=2020020202&oe=20202020202",
        "object_id": "30303030303",
        "id": "10101010101010101010"
      },
     ....
 ]}
}

所以你从“数据”中得到一个空数组,因为数据不在响应键中。

请尝试以下

            public void onCompleted(GraphResponse response) {
                ...
                try {
                    JSONObject jsonObject = response.getJSONObject().getJSONObject("posts");
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    ...
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ....
            }