Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK for android: getFirstName() and getLastName() return null strings

I'm developing an android native app using Facebook SDK 3.5.

I'm trying to have an autocompletetextview where I can pick some facebook friends, for this I'm using an newMyFriendsRequest as below:

private void fbFriendsRequest(final Session session) {
    Request request = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {
        @Override
        public void onCompleted(List<GraphUser> listFacebookFriends, Response response) {
            // If the response is successful
            if (session == Session.getActiveSession()) {
                if (listFacebookFriends != null) {
                    mAdapter = new GraphUserAdapter(FriendPicker.this, listFacebookFriends);
                    mAutoComplete.setAdapter(mAdapter);
                    Toast.makeText(FriendPicker.this,"Friends Loaded",Toast.LENGTH_SHORT).show();
                }
            }
            if (response.getError() != null) {
                // Handle errors, will do so later.
            }
        }
    });
    request.executeAsync();
}

Then each time the user pick a row from the autocompletetextview I add this row to another list, and when a I press OK button I have:

public void onOkClick(View v) {
    if(mGraphUsersList.size()==0) {
        setResult(RESULT_CANCELED,null);    
    } else {
        Intent returnIntent = new Intent();
        Iterator<GraphUser> itr = mGraphUsersList.iterator();
        int n = mGraphUsersList.size();
        String[] idfb = new String[n];
        String[] names = new String[n];
        String[] lastnames = new String[n];
        int i = 0;
        while(itr.hasNext()) {
            GraphUser User = (GraphUser) itr.next();
            idfb[i]=User.getId();
            names[i]=User.getFirstName();    // problem
            lastnames[i]=User.getLastName(); // problem
            i++;
        }
        returnIntent.putExtra("idfb",idfb);
        returnIntent.putExtra("names",names);
        returnIntent.putExtra("lastnames",lastnames);
        setResult(RESULT_OK,returnIntent);     
    }
    finish();
}

When I use the getFirstName() and getLastName() functions I have null strings, but if I use getName() function it's return the name+lastname. It's like if in the list given by newMyFriendsRequest something has been lost.

Anyone knows a workaround for this?

like image 544
GerryR Avatar asked Nov 17 '13 11:11

GerryR


1 Answers

To get other fields than id and name of friends, you need to specify them explicitly in your request.

For this add them to the Bundle of your request like:

Request request = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {

    @Override
    public void onCompleted(List<GraphUser> listFacebookFriends, Response response) {
        // your code like you did
    }
}

// here add fields explicitly 
Bundle bundle = request.getParameters();
mBundle.putString("fields", "id,first_name,last_name");

// execute like you did
request.executeAsync();

This is one solution for you, or another one is to use this super simple library: android-simple-facebook

like image 73
sromku Avatar answered Oct 01 '22 16:10

sromku