2016-02-12 67 views
-1

我正在整合Facebook登录我的Android应用程序 我已成功登录到Facebook从应用程序和成功后,我打开一个活动。获取facebook用户名称在Android应用程序

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

      // String fbname = profile.getName(); 
      //AppLog.Log("name",); 
      Intent intent = new Intent(getApplicationContext(), ComputerCategoryActivity.class); 
      startActivity(intent); 


     } 

现在我想发送Facebook个人资料的用户名。对于这一点,如果我从上面的代码中删除注释,并试图通过这条线得到它:

String fbname = profile.getName(); 

我登录到FB,但因此它是进入onSuccess()方法的应用是不开放的活动。请帮我为什么不能用Profile.getCurrentProfile();

+0

实际上它没有崩溃但没有打开指定的活动,这意味着onSuccess()未执行 –

回答

1

你应该通过这种方式太明白:

 @Override 
     public void onSuccess(LoginResult loginResult) { 
      final AccessToken accessToken = loginResult.getAccessToken(); 
      GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject user, GraphResponse graphResponse) { 

        // user.optString("name")); 
        // user.optString("id")); 
        // user.optString("email"));       
       } 
      }).executeAsync(); 
     } 

获得简介PIC位图:

public static Bitmap getFacebookProfilePicture(String userID){ 
    URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large"); 
    Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream()); 

    return bitmap; 
} 
+0

它的工作。还有一个问题:如何获取profilepicture网址? –

+0

thnx很多... :) –

+0

不客气:) –

0

请试试这个,在方法的onSuccess -

final GraphRequest request = GraphRequest.newMeRequest(
            loginResult.getAccessToken(), 
            new GraphRequest.GraphJSONObjectCallback() { 
             @Override 
             public void onCompleted(
               JSONObject object, 
               GraphResponse response) { 

              Log.d("Response", response.getJSONObject().toString()); 

              if (response.getError() != null) { 
               // handle error 
               System.out.println("Error from FB "); 

              } else { 
               try { 

    //            JSONObject _jObject = new JSONObject(response.toString()); 
                if (response.getJSONObject().toString() != null) {              
                 if (response.getJSONObject().has("name")) { 
                  String _facebookName = response.getJSONObject().getString("name"); 



                 } 



                } 

               } catch (Exception e) { 
                System.out.println("JSON Error"); 
                e.printStackTrace(); 
               } 


              } 
             } 
            }); 
相关问题