0

我想从 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");}
    }
}

当我从课堂上转到该OnComplete方法时,它给出了一个 Facebook 错误......IDRequestListener

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

4

2 回答 2

0

您可以使用 socialauth android sdk。可以获取用户资料,发布消息,获取好友列表

http://code.google.com/p/socialauth-android/

于 2012-09-12T10:41:38.237 回答
0

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

此外,您的活动似乎缺少为onActivityResult()对象Facebook提供授权结果的实现,即

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  mFacebook.authorizeCallback(requestCode, resultCode, data);
}
于 2012-09-12T11:01:52.627 回答