![](/img/trans.png)
[英]unable to fetch the facebook user profile after login with facebook in android
[英]Android facebook after login get user information
我想用facebook登錄后獲取用戶信息。
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_birthday", "email");
final Context contextM = this.getApplicationContext();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String firstName = Profile.getCurrentProfile().getFirstName();
String lastName = Profile.getCurrentProfile().getLastName();
String url = Profile.getCurrentProfile().getProfilePictureUri(100, 100).toString();
final ImageView mImageView = (ImageView) findViewById(R.id.imageView);
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(contextM).addToRequestQueue(request);
}
這是我得到的錯誤:
java.lang.NullPointerException:嘗試在空對象引用上調用虛方法'java.lang.String com.facebook.Profile.getFirstName()'
您似乎錯過了public_profile權限,它允許您同時獲取first_name和姓氏。
只需使用getRecentlyGrantedPermissions()檢查您獲得的所有權限。
我使用圖形請求訪問了如下結果。 檢查這是否對您有所幫助
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>(){
@Override
public void onSuccess(final LoginResult loginResult){
GraphRequest request=GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(
JSONObject object,
GraphResponse response){
if(response.getError()!=null){
// handle error
}else{
//**access details as follows**
String email=object.optString("email");
String first_name=object.optString("first_name");
String last_name=object.optString("last_name");
}
});
Bundle parameters=new Bundle();
parameters.putString("fields","id,first_name,last_name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel(){
//Toast.makeText(context, "Something went wrong. Please try again",
// Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception){
//Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.