2

我正在使用结构将 Twitter 集成到 Android 应用程序中。

public class MainActivity extends AppCompatActivity {

    private static final String TWITTER_KEY = "";
    private static final String TWITTER_SECRET = "";
    private TwitterLoginButton loginButton;
    private Button btnPostTweet;
    private static final int TWEET_COMPOSER_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));
        setContentView(R.layout.activity_main);

        loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        btnPostTweet = (Button) findViewById(R.id.btn_post_tweet);
        btnPostTweet.setOnClickListener(onClickListener);
        loginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
            Twitter.getInstance().core.getSessionManager().getActiveSession()
                TwitterSession session = result.data;
                String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(TwitterException exception) {
                Log.d("TwitterKit", "Login with Twitter failure", exception);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Make sure that the loginButton hears the result from any
        // Activity that it triggered.
        if (requestCode == TWEET_COMPOSER_REQUEST_CODE && resultCode == RESULT_OK)
            Toast.makeText(MainActivity.this, "Updated tweet using composer", Toast.LENGTH_SHORT).show();
        else
            loginButton.onActivityResult(requestCode, resultCode, data);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_post_tweet:
                    postTweet();
//                    postTweetManually();
                    break;
                default:
                    break;
            }
        }
    };

    private void postTweet() {
       /* TweetComposer.Builder builder = new TweetComposer.Builder(this)
                .text("just setting up my Fabric.");
        Intent twitterIntent = builder.createIntent();
        startActivityForResult(twitterIntent, REQUEST_TWEET_POST);*/

        Intent intent = null;
        try {
            intent = new TweetComposer.Builder(this)
                    .text("Tweet from Fabric!")
                    .url(new URL("http://www.twitter.com"))
                    .createIntent();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        startActivityForResult(intent, TWEET_COMPOSER_REQUEST_CODE);
    }

    private void postTweetManually() {
        TwitterSession twitterSession = Twitter.getSessionManager().getActiveSession();
        StatusesService statusesService = Twitter.getApiClient(twitterSession).getStatusesService();
        String username = Twitter.getSessionManager().getActiveSession().getUserName();
        statusesService.update("@" + username + "Manually update on twitter1", 1L, true, 0.0d, 0.0d, "", true, true, new Callback<Tweet>() {
            @Override
            public void success(Result<Tweet> result) {
                Toast.makeText(MainActivity.this, "Tweet Updated", Toast.LENGTH_LONG).show();
                Log.d("Tweet Updated", result.data.user.name);
            }

            @Override
            public void failure(TwitterException e) {
                Log.d("Tweet Update Failed", e.getMessage());
            }
        });
    }
}

我没有在我的设备中安装 Twitter 应用程序。

所以 TwitterComposer 正在打开 WebBroswer。

发布推文后,我得到如下屏幕,它不会重定向到应用程序。

在此处输入图像描述

注意:登录时它可以完美运行..

谢谢。

4

1 回答 1

2

不确定您是否遇到与我相同的问题,但在我的情况下,我收到了 onActivityResult() 回调,但 resulCode 不是 RESULT_OK 尽管推文已成功发布

于 2016-06-09T11:01:58.230 回答