在facebook响应中获取null graphobject

时间:2014-06-20 13:08:14

标签: android facebook

我正在尝试通过他们的个人资料图片获取facebook好友列表,但我没有取得任何成功。这是代码

public class MainActivity extends Activity {

private final static String TAG = "MainActivity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   

    Session.openActiveSession(this, true, new Session.StatusCallback(){

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {

            requestMyAppFacebookFriends(session);

        }

    });


  }

  private Request createRequest(Session session) {
        Request request = Request.newGraphPathRequest(session, "me/friends", null);

        Set<String> fields = new HashSet<String>();
        String[] requiredFields = new String[] { "id", "name", "picture"};
        fields.addAll(Arrays.asList(requiredFields));

        Bundle parameters = request.getParameters();
        parameters.putString("fields", TextUtils.join(",", fields));
        request.setParameters(parameters);

        return request;
    }


  private void requestMyAppFacebookFriends(Session session) {
        Request friendsRequest = createRequest(session);
        friendsRequest.setCallback(new Request.Callback() {

            @Override
            public void onCompleted(Response response) {
                List<GraphUser> friends = getResults(response);
               Log.v(TAG, "friends list size "+friends.size());
            }
        });
        friendsRequest.executeAsync();
    }

  private List<GraphUser> getResults(Response response) {
        GraphMultiResult multiResult = response.getGraphObjectAs(GraphMultiResult.class);
        GraphObjectList<GraphObject> data = multiResult.getData();
        return data.castToListOf(GraphUser.class);
    }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }

我面临的问题是,当我尝试获取graphobject response.getGraphObjectAs(GraphMultiResult.class)时,它返回null。因此,请指导我在这里做错了什么,或者还有其他任何方式来获取Facebook朋友。

1 个答案:

答案 0 :(得分:0)

您可能正在使用Graph API的v2.0。有了这个,你只会得到也使用相同应用程序的朋友。

请参阅retrieve full list of friends using facebook API