2016-02-12 68 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(); 
               } 


              } 
             } 
            }); 
相關問題