2012-09-12 47 views
0

我想从Facebook的用户得到的ID,但什么至今不工作我已经试过......我如何从Facebook用户获取用户ID?

public class fbLogin extends Activity { 

    public static final int LOGIN = Menu.FIRST; 
    public static final int GET_EVENTS = Menu.FIRST + 1; 
    public static final int GET_ID = Menu.FIRST + 2; 

    public static final String APP_ID = "361579407254212"; 
    public static final String TAG = "FACEBOOK CONNECT"; 

    private Facebook mFacebook; 
    private AsyncFacebookRunner mAsyncRunner; 
    private Handler mHandler = new Handler(); 

    private static final String[] PERMS = new String[] { "user_events","email" }; 

    private TextView mText; 

    /** Called when the activity is first created. */ 

    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      super.onCreate(savedInstanceState); 

      // setup the content view 
      initLayout(); 

      // setup the facebook session 
      mFacebook = new Facebook(APP_ID); 
      mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

      if (mFacebook.isSessionValid()) { 
       AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook); 
       asyncRunner.logout(this, new LogoutRequestListener()); 
      } else { 
       mFacebook.authorize(this, PERMS, new LoginDialogListener()); 
      } 

      mAsyncRunner.request("me", new IDRequestListener());  
    } 

    protected void initLayout() { 
     LinearLayout rootView = new LinearLayout(this.getApplicationContext()); 
     rootView.setOrientation(LinearLayout.VERTICAL); 

     this.mText = new TextView(this.getApplicationContext()); 
     this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
     LayoutParams.WRAP_CONTENT)); 
     rootView.addView(this.mText); 

     this.setContentView(rootView); 

    } 


    private class LoginDialogListener implements DialogListener { 

     public void onComplete(Bundle values) {}  

     public void onFacebookError(FacebookError e) {} 

     public void onError(DialogError e) {} 

     public void onCancel() {} 
    } 

    private class LogoutRequestListener implements RequestListener { 

     public void onComplete(String response, Object state) { 
      // Dispatch on its own thread 
      mHandler.post(new Runnable() { 
       public void run() { 
        mText.setText("Logged out"); 
       } 
      }); 
     } 

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

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

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

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

    private class IDRequestListener implements RequestListener { 

     public void onComplete(String response, Object state) { 
      try { 
       // process the response here: executed in background thread 
       Log.d(TAG, "Response: " + response.toString()); 
       JSONObject json = Util.parseJson(response); 
       final String id = json.getString("id"); 

       fbLogin.this.runOnUiThread(new Runnable() { 
        public void run() { 
         mText.setText("Hello there, " + id + "!"); 
        } 
       }); 
      } catch (JSONException e) { 
       Log.w(TAG, "JSON Error in response"); 
       mText.setText("catch1..."); 
      } catch (FacebookError e) { 
       mText.setText("catch2"); 
      } 
     } 

     public void onIOException(IOException e, Object state) {mText.setText("Logging out...1");} 

     public void onFileNotFoundException(FileNotFoundException e, Object state) {mText.setText("Logging out...2");} 

     public void onMalformedURLException(MalformedURLException e,Object state) {mText.setText("Logging out...3");} 

     public void onFacebookError(FacebookError e, Object state) {mText.setText("Logging out...4");} 
    } 
} 

它给出了一个Facebook的错误,当我去到OnComplete方法从IDRequestListener类...

我该如何解决它?任何人都可以帮助我吗?

回答

0

authorize()不同步,将立即返回。在提交需要访问令牌的请求之前,您需要等待授权请求完成。基本上,只有在authorize()侦听器中调用onComplete()时,才会触发me请求。

而且你的活动似乎缺少onActivityResult()实现,饲料与授权结果Facebook对象,即

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