0

我使用 developer.facebook.com 提供的基本 Facebook 库制作了一个在 Facebook 上发布的示例,它与 SSO 配合得很好,

    btnPostOnFb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        {
                facebook.authorize(FBIntegrationSampleActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"}, new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                        if(values!=null && values.containsKey("access_token")){
                            postOnWall("NEW POST from" +count+"Android -Anuj");
                            Log.e("post on wall", "WALLPOST");
                            Toast.makeText(getApplicationContext(), "SUCCESSFULLY POSTED MSG ON WALL", Toast.LENGTH_SHORT).show();
                        }else if(values!=null)
                            Log.e("LOGINE SUCCESS", "LOGIN SUCCESS");
                            Toast.makeText(getApplicationContext(), "SUCCESSFULLY LOGGED IN", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFacebookError(FacebookError error) {
                        Log.e("onFBERROR", "ONFBERROR");
                    }
                    @Override
                    public void onError(DialogError e) {
                        Log.e("on DESI ERROR", "ON_ERROR");
                    }
                    @Override
                    public void onCancel() {
                        Log.e("onCANCEL", "ONCANCEL");
                    }
                });
            }
        }
    });

哪个成功发布在 Facebook 墙上,我想要的是,我需要向用户显示他已成功登录,然后会在那里发布一条消息。

我面临的问题是onComplete(Bundle values)成功登录和成功发布都调用了该方法,我如何区分它们,keyBundle 值中是否有一个可以帮助找到差异的方法?

欢迎任何建议。

4

2 回答 2

1

问题是您用于身份验证和发布。无需这样做:

用于身份验证

facebook.authorize(a, PERMISSIONS,-1,new LoginListener());

并发布:

1)没有对话框facebook.request(parameters)

2) 带对话框

facebook.dialog(this,"stream.publish",parameters,new TestUiServerListener());


    public class TestUiServerListener implements DialogListener {
    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            new AsyncFacebookRunner(ZValues.authenticatedFacebook).request(postId,new TestPostRequestListener());
        } else {
            Post_Message_Title.this.runOnUiThread(new Runnable() {
                public void run() {
                }
            });
        }
    }

    public void onCancel() {
    }

    public void onError(DialogError e) {
        e.printStackTrace();
    }

    public void onFacebookError(FacebookError e) {
        e.printStackTrace();
    }
}

public class TestPostRequestListener implements RequestListener {
    public void onComplete(final String response, final Object state) {
        try {
            JSONObject json = Util.parseJson(response);
            String postId = json.getString("id");
            this.runOnUiThread(new Runnable() {
                public void run() { 
                        successLoginShowDialog();  // Dialog after Login succeeds
                }
            });
        } catch (Throwable e) {
        }
    }

    public void onFacebookError(FacebookError e, final Object state) {
        e.printStackTrace();
    }

    public void onFileNotFoundException(FileNotFoundException e,
            final Object state) {
        e.printStackTrace();
    }

    public void onIOException(IOException e, final Object state) {
        e.printStackTrace();
    }

    public void onMalformedURLException(MalformedURLException e,
            final Object state) {
        e.printStackTrace();
    }
}

只需创建一个方法 successLoginShowDialog() 并显示您想要的任何内容,

如果 Post 成功,在 TestPostRequestListener 下面的 Thread 将被调用,所以在这个 Thread 中做所有的东西:

this.runOnUiThread(new Runnable() {
        public void run() { 
            successLoginShowDialog();  // Dialog after Login succeeds
        }
});
于 2012-06-19T12:01:34.760 回答
0

到 postOnWall() 你可以得到它的回应:

public void postToWall(String message) {
        Bundle parameters = new Bundle();
        parameters.putString("message", message);
        parameters.putString("description", "topic share");
        try {
            fbObj.request("me");
            String response = fbObj.request("me/feed", parameters, "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("")
                    || response.equals("false")) {
                // showToast("Blank response.");
                Toast.makeText(context, "blank response", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // showToast("Message posted to your facebook wall!");
                Toast.makeText(context,
                        "Message posted to your facebook wall!",
                        Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            // showToast("Failed to post to wall!");
            e.printStackTrace();

        }
    }
于 2012-06-19T11:45:26.883 回答