2014-01-20 81 views
0

我在android上使用facebook sdk,我想檢查用戶ID(例如在吐司上顯示userID) 我寫了一些代碼,可以在Facebook上登錄,現在我想登錄用戶時我想土司android facebook登錄並檢查用戶ID

這是我的代碼來顯示用戶ID,

public void loginToFacebook() { 

    mPrefs = getPreferences(MODE_PRIVATE); 
    String access_token = mPrefs.getString("access_token", null); 
    long expires = mPrefs.getLong("access_expires", 0); 

    if (access_token != null) { 
     facebook.setAccessToken(access_token); 

     Intent in = new Intent(getApplicationContext(), 
       ChooseLocation.class); 
     startActivity(in); 

     Log.d("FB Sessions", "" + facebook.isSessionValid()); 
    } 

    if (expires != 0) { 
     facebook.setAccessExpires(expires); 
    } 

    if (!facebook.isSessionValid()) { 
     facebook.authorize(this, 
       new String[] { "email", "publish_stream" }, 
       new DialogListener() { 

        @Override 
        public void onCancel() { 

        } 

        @Override 
        public void onComplete(Bundle values) { 

         SharedPreferences.Editor editor = mPrefs.edit(); 
         editor.putString("access_token", 
           facebook.getAccessToken()); 
         editor.putLong("access_expires", 
           facebook.getAccessExpires()); 
         editor.commit(); 
         try { 
          JSONObject me = new JSONObject(facebook 
            .request("me")); 
          String id = me.getString("id"); 
          Toast.makeText(getApplicationContext(), id, 
            Toast.LENGTH_SHORT).show(); 
         } catch (Exception e) { 
          // TODO: handle exception 
         } 
         Intent in = new Intent(getApplicationContext(), 
           ChooseLocation.class); 
         startActivity(in); 

        } 

        @Override 
        public void onError(DialogError error) { 

        } 

        @Override 
        public void onFacebookError(FacebookError fberror) { 

        } 

       }); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    facebook.authorizeCallback(requestCode, resultCode, data); 

} 

}

回答

1

你可以調用這個函數來獲取用戶信息時,他/她成功登錄。

public void getProfileInformation() { 
    mAsyncRunner.request("me", new RequestListener() { 
     @Override 
     public void onComplete(String response, Object state) { 
      Log.d("Profile", response); 
      String json = response; 
      try { 
       JSONObject profile = new JSONObject(json); 
        // getting id of the user 
       final String id = profile.getString("id"); 
       // getting name of the user 
       final String name = profile.getString("name"); 


       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), "Name: " + name + "\nID: " + id, Toast.LENGTH_LONG).show(); 
        } 

       }); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onIOException(IOException e, Object state) { 
     } 

     @Override 
     public void onFileNotFoundException(FileNotFoundException e, 
       Object state) { 
     } 

     @Override 
     public void onMalformedURLException(MalformedURLException e, 
       Object state) { 
     } 

     @Override 
     public void onFacebookError(FacebookError e, Object state) { 
     } 
    }); 
} 

請參閱Log.d("Profile", response);在LogCat中。它返回Json。 我認爲這會幫助你。

+0

謝謝,我可以在我的代碼中調用這個getProfileInformation函數? –

+0

onComplete中的任何地方(捆綁值) –

+0

謝謝你工作,但我想通過intent.putextra顯示用戶id另一個活動,但我的問題是,當我點擊登錄putextra值爲null(例如textview的settext是你好),但是當我點擊返回按鈕,然後點擊登錄程序在此刻工作 –