2015-10-18 101 views
0

我想在应用程序中获取通过Facebook连接的人员的姓名。我在这个方法中使用成功:Android:使用GraphRequest获取并存储Facebook名称newMeRequest

private LoginButton loginbutton2; 
    private ProfilePictureView fAvatar2; 
    private TextView fName2; 

private FacebookCallback<LoginResult> mCallback2 = new FacebookCallback<LoginResult>() { 
     public void onSuccess(LoginResult loginResult) { 
      AccessToken accessToken = loginResult.getAccessToken(); 
      final Profile profile = Profile.getCurrentProfile(); 

      GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) { 
        /*fName2.setText(profile.getName()); 
        fAvatar2.setProfileId(profile.getId());*/ 
       } 
      }).executeAsync(); 
      } 

} 

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     setContentView(R.layout.activity_info_account); 

     mCallbackManager2 = CallbackManager.Factory.create(); 
     loginbutton2 = (LoginButton) findViewById(R.id.login_button2); 
     loginbutton2.setBackgroundResource(R.drawable.facebookconnexion); 
     loginbutton2.setReadPermissions("user_friends"); 
     loginbutton2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
     loginbutton2.registerCallback(mCallbackManager2, mCallback2); 


     fName2 = (TextView) findViewById(R.id.facebookName2); 
     fAvatar2 = (ProfilePictureView) findViewById(R.id.facebook_avatar2); 
} 

如何将名称放入我的textview(评论中)?当我不评论此行时:/*fName2.setText(profile.getName());我有一个错误ENOENT。 事实上,我如何使用newMeRequest存储名称,以便我将其放入textview之后?

THX,

回答

0

为了让名字,这样做

在这个地方

GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 
      @Override 
      public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) { 
       try { 
fName2.setText(jsonObject.getString("first_name")); 
       fAvatar2.setProfileId(jsonObject.getString("profile_id"));<--I am not sure profile_id is the name there do this to see the JSON response 
Log.e("FBGraphResponse", jsonObject.toString()); 
} catch(Exception e){e.printStackTrace();} 
      } 
     }).executeAsync(); 
+0

谢谢,这就是我想要的,但我对jsonObject.getString错误( “first_name”):未处理的异常:org.json.JSONException – Jey10

+0

更新...并记住要upvote答案 –

相关问题