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

2

5

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?

Modality answered 17/11, 2013 at 11:31 Comment(0)
U
10

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

Unyoke answered 17/11, 2013 at 20:31 Comment(4)
Thank you! I've changed a little bit the code with: Bundle bundle = new Bundle(); bundle.putString("fields", "id,first_name,last_name"); request.setParameters(bundle);Modality
@Unyoke where to find this kind of information regarding Facebook SDK ? How can one know what's default available and what needs to added explicitly ?Angeles
@Jabbar_Jigariyo I am afraid to say, that there is no such information. I think that I read 99% of all their docs. I found this by playing a lot with graph api explorer. For example if you run /me/friends you will get only id and name for your friends. Also, saw many code samples that do the same. Maybe one day, facebook will have better documentation to cover this info.Unyoke
@Unyoke Thanks for the info. I also took a look at your github library. It seems very useful.Angeles
C
0
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {

                    Profile profile = Profile.getCurrentProfile();
                    Log.d("fbEmail_id", profile.getFirstName());
                    String first_name=profile.getFirstName();
                    String last_name=profile.getLastName();
                }
Couchant answered 29/4, 2020 at 17:0 Comment(1)
HI! where is Profile coming from? Please add some comments/explanation to your code for clarity.Groceries

© 2022 - 2024 — McMap. All rights reserved.