问题
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.
回答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.
回答2:
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..
回答3:
This was the solution
String response = facebook.request("me", param, "GET");
Hope it helps someone who had similar problem.
来源:https://stackoverflow.com/questions/15134978/how-to-get-id-and-name-from-facebook-profile-using-facebook-sdk