0

I know that the way is using JSON and i have read much about how to do it, but i still can't figure out WHERE i have to put the JSON code so i can get the id and name, because everything i tried didn't work (was null). Can you guys give me an advice of where i shall request id and name so i can actually get them? This is everything in my main class related to facebook:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}

@Override
protected void onResume() {
    super.onResume();
    updateLoginStatus();
}

public void updateLoginStatus() {
    loginStatus.setText("Logged into Facebook as "+        facebookConnector.getUserName() + facebookConnector.getFacebook().isSessionValid());
}

public void postMessage() {

    if (facebookConnector.getFacebook().isSessionValid()) {
        postMessageInThread();
    } else {
        SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {

            @Override
            public void onAuthSucceed() {
                postMessageInThread();
            }

            @Override
            public void onAuthFail(String error) {

            }
        };
        SessionEvents.addAuthListener(listener);
        facebookConnector.login();

    }
}

private void postMessageInThread() {
    Thread t = new Thread() {
        public void run() {

            try {
                facebookConnector.postMessageOnWall("test");
                mFacebookHandler.post(mUpdateFacebookNotification);
            } catch (Exception ex) {
                Log.e(TAG, "Error sending msg",ex);
            }
        }
    };
    t.start();
}

private void clearCredentials() {
    try {
        facebookConnector.getFacebook().logout(getApplicationContext());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Last i tried with

 AsyncFacebookRunner myAsyncRunner = new AsyncFacebookRunner(facebook);
    myAsyncRunner.request("me", new meRequestListener());

and then

 public class meRequestListener implements RequestListener {

    public void onComplete(String response, Object state) {

        fbName = response;
    }

when i get the string with

public String getUserName () {
    return fbName;
}

it is still null. Help please.

3 Answers3

1

u can use this: (this code is using Facebook SDK 3.02)

user id:

final Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // If the session is open, make an API call to get user data
        // and define a new callback to handle the response
        Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                // If the response is successful
                if (session == Session.getActiveSession()) {
                    if (user != null) {
                        user_ID = user.getId();//user id
                        profileName = user.getName();//user's profile name
                        userNameView.setText(user.getName());
                    }   
                }   
            }   
        }); 
        Request.executeBatchAsync(request);
    }  

user_ID & profileName are string.

Shoshi
  • 2,254
  • 1
  • 29
  • 43
  • Ok i made it work and added it in the login() method but i still get empty toast and null in the textview. – Крис Беров Feb 28 '13 at 14:10
  • have u get the id and set the textfield in the onComplete method ? – Shoshi Feb 28 '13 at 14:16
  • Like i said i put that code in the FacebookConnector.java in login() method and i get the name and id with getUserName() and getUserId() so i can set them in my main activity. I can't set the textfield there because its not an activity. – Крис Беров Feb 28 '13 at 14:42
  • the code i have given will take some time to get user_ID and userProfileName because of internet. if u want to do something with user_id, then u have to get it first in onComplete() method. that mean u have to call a function (in which function u write your desire code) after `user_ID = user.getId();` this line. – Shoshi Feb 28 '13 at 15:02
  • Yes i make sure to get the name and id first before i use them. I force a login() at a button click which tosses me the toast and that toast is now "null null" instead of empty. Still doesnt work as wanted. – Крис Беров Feb 28 '13 at 15:11
  • in my code, u have to login first then u can get what u want like user id username etc – Shoshi Feb 28 '13 at 16:15
  • yea i do the same. The only diff is i am already logged in, but that shouldn't be the problem since i force the request at every startup. – Крис Беров Feb 28 '13 at 16:38
  • i have give this same answer to this link: http://stackoverflow.com/questions/15127783/facebook-sdk-3-0-get-facebook-user-id-and-access-token/15128174#15128174 this help him well, i cannot find out what is your code's problem which version of sdk r u using ? – Shoshi Feb 28 '13 at 17:13
1

This was the solution

String response = facebook.request("me", param, "GET");

Hope it helps someone who had similar problem.

0

Try this:

AsyncFacebookRunner myAsyncRunner = new AsyncFacebookRunner(facebook);
JSONObject json = Util.parseJson(myAsyncRunner.request("me", new meRequestListener()));
                    String facebookID = json.getString("id");
                    String firstName = json.getString("name");                  
                    System.out.println("Details: "+id+" "+name);

I hope this will be help to you..

Android_coder
  • 9,953
  • 3
  • 17
  • 23