2011-03-28 34 views
1

我有一個行代碼的Facebook與Android - 獲取ID

String id = facebook.request("me"); 
     System.out.println(id); 

這將返回所有的細節,但我不知道怎麼回的人就在ID?

任何想法?

感謝

+0

告訴我們你在你的println中得到了什麼。 – 2011-03-28 09:24:09

回答

3
JSONObject jObject = new JSONObject(authenticatedFacebook.request("me"));  
String id =jObject.getString("id"); 

這將返回的記錄式User..Similarly的ID可以通過更換標籤名稱獲取所有數據..

+0

非常感謝 – James 2011-03-28 09:27:52

0

In the documentation它說,你可以要求"me?fields=id"代替"me",以便只傳輸所​​需的數據。

然而,使用Android API mAsyncRunner.request("me?fields=id", ...)導致以下錯誤,即使在"me"正常工作相同的情況:

{ 
    "error": { 
    "message": "An active access token must be used to query information about the current user.", 
    "type": "OAuthException", 
    "code": 2500 
    } 
} 

,所以我想獲得的信息一大堆,並提取ID爲Venky解釋是最好的方法。

2

實際上,Lena的答案更有效率,可以工作,但缺少一點... fields = id應該作爲新的Bundle對象傳遞。然後你只能在Facebook的回覆中獲得ID。

Bundle bundle = new Bundle(); 
bundle.putString("fields", "id"); 
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(facebook); 
asyncRunner.request("me", bundle, new AsyncFacebookRunner.RequestListener() { 
      public void onMalformedURLException(MalformedURLException e, Object state) { 
       // TODO Auto-generated method stub 
      } 

      public void onIOException(IOException e, Object state) { 
       // TODO Auto-generated method stub 
      } 

      public void onFileNotFoundException(FileNotFoundException e, Object state) { 
       // TODO Auto-generated method stub 
      } 

      public void onFacebookError(FacebookError e, Object state) { 
       // TODO Auto-generated method stub 
      } 

      public void onComplete(String response, Object state) { 
       JSONObject jObject; 
       try { 
        jObject = new JSONObject(response); 
        Log.d("FACEBOOK ID", jObject.getString("id")); 

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