import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.MenuItem;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.widget.LoginButton;
import android.widget.Toast;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
public class MainActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private LoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.login);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "MAGNETOB.TTF");
TextView myTextview = (TextView) findViewById(R.id.textview1);
myTextview.setTypeface(myTypeface);
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Facebook Login Successful!");
System.out.println("Logged in user Details : ");
System.out.println("--------------------------");
System.out.println("User ID : " + loginResult.getAccessToken().getUserId());
System.out.println("Authentication Token : " + loginResult.getAccessToken().getToken());
Toast.makeText(MainActivity.this, "Login Successful!" + loginResult.getAccessToken().getUserId() , Toast.LENGTH_LONG).show();
*** // Over here where it says get.UserId()***
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login cancelled by user!", Toast.LENGTH_LONG).show();
System.out.println("Facebook Login failed!!");
}
@Override
public void onError(FacebookException e) {
Toast.makeText(MainActivity.this, "Login unsuccessful!", Toast.LENGTH_LONG).show();
System.out.println("Facebook Login failed!!");
}
});
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
callbackManager.onActivityResult(reqCode, resCode, data);
}
在我的onSucess方法中,我想使用toast打印用户名,但似乎无法找到如何在没有textview的情况下这样做,这不是我想要的,我想使用toast加载用户名,但目前我正在加载userID。
请帮助谢谢
答案 0 :(得分:0)
@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if(profile != null) {
String fbid = profile.getId();
String name = profile.getFirstName();
String profileUrl = "https://graph.facebook.com/" + fbid
+ "/picture?type=large";
}
}
@Override
public void onSuccess(final LoginResult loginResult) {
accessToken = loginResult.getAccessToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
//show error message to user
} else {
loginResult.getRecentlyGrantedPermissions();
Log.d("fb response", json.toString());
try {
//check json response
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,location");
request.setParameters(parameters);
request.executeAsync();
}
答案 1 :(得分:0)
在onSuccess
方法中,使用Graph API获取用户信息,请参阅official doc
@Override
public void onSuccess(LoginResult loginResult) {
Bundle params = new Bundle();
params.putString("fields", "id,name,first_name,last_name");//here you can add more fields as you want
new GraphRequest(loginResult.getAccessToken(), "me"/*you can also pass FB id of user instead of 'me'*/, params, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
if (response != null) {
try {
JSONObject userObj = response.getJSONObject();
String fname =userObj.optString("first_name");
String lname =userObj.optString("last_name");
String fullName =userObj.optString("name");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}