我知道方式是使用JSON,我已经阅读了很多关于如何做到这一点,但我仍然无法弄清楚我必须把JSON代码放在哪里,所以我可以获得id和名称,因为我所有的一切尝试不起作用(为空)。你能不能给我一个建议,我会在哪里申请id和名字,这样我才能真正得到它们? 这是我主要课程中与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();
}
}
最后我尝试了
AsyncFacebookRunner myAsyncRunner = new AsyncFacebookRunner(facebook);
myAsyncRunner.request("me", new meRequestListener());
然后
public class meRequestListener implements RequestListener {
public void onComplete(String response, Object state) {
fbName = response;
}
当我用
获取字符串时public String getUserName () {
return fbName;
}
它仍然是空的。请帮助。
答案 0 :(得分:1)
用户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
是字符串。
答案 1 :(得分:0)
试试这个:
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);
我希望这会对你有所帮助..
答案 2 :(得分:0)
这是解决方案
String response = facebook.request("me", param, "GET");
希望能帮助有类似问题的人。