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

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 に答える
0

socialauth android sdk を使用できます。ユーザー プロフィールの取得、メッセージの投稿、フレンド リストの取得が可能

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

于 2012-09-12T10:41:38.237 に答える