我希望通过这个简单的应用程序显示我的Facebook名称,该应用程序在点击按下login()
的按钮后登录。
会话正确打开,但Request.GraphUserCallback不起作用,似乎返回null。
当我启动我的应用程序时,LogCat没有显示任何错误,但是当Graph API返回用户时,我在onCompleted上显示的Toast上看不到我的user.name()
。
这是我没有导入而没有布局的代码:
public class MainActivity extends Activity {
private Session.StatusCallback statusCallback;
private Request.GraphUserCallback requestGraphUserCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Ci siamo", Toast.LENGTH_SHORT).show();
statusCallback= new SessionStatusCallback();
requestGraphUserCallback = new RequestGraphUserCallback();
}
public void login(View view){
Toast.makeText(getApplicationContext(), "Login", Toast.LENGTH_SHORT).show();
// start Facebook Login
Session.openActiveSession(this, true, statusCallback);
Toast.makeText(getApplicationContext(), "finito login", Toast.LENGTH_SHORT).show();
}
//CLASSE SESSIONE
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, requestGraphUserCallback);
}
}
}
//CLASSE GRAPH
private class RequestGraphUserCallback implements Request.GraphUserCallback{
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(getApplicationContext(), user.getName(), Toast.LENGTH_SHORT).show();
}
}
}
}
答案 0 :(得分:6)
您没有执行请求。在SessionStatusCallback中,在创建请求后,您应该在其上调用executeAsync()
方法。