0

我正在尝试将Facebook和集成Twitter到我的应用程序中。当用户单击按钮时,我将显示一个对话框,其中包含 Facebook 和 Twitter 两个选项。当用户点击 Twitter 时,我正在关闭对话框并调用 Twitter PrepareRequestToken activity

这是我的 PrepareRequestToken 类:

public class PrepareRequestTokenActivity extends Activity {

    final String TAG = getClass().getName();
    private Twitter twitter;
    private CommonsHttpOAuthConsumer consumer;
    private OAuthProvider provider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getConsumerProvider();
        try {
            consumer = new CommonsHttpOAuthConsumer(ConstantValues.CONSUMER_KEY, ConstantValues.CONSUMER_SECRET);
            provider = new DefaultOAuthProvider(
                    "https://api.twitter.com/oauth/request_token",
                    "https://api.twitter.com/oauth/access_token",
                    "https://api.twitter.com/oauth/authorize");
            String authUrl = provider.retrieveRequestToken(consumer, ConstantValues.CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
            setConsumerProvider();
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


    @Override
    public void onNewIntent(Intent intent) {

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        final Uri uri = intent.getData();
        if (uri != null && uri.getScheme().equals(ConstantValues.OAUTH_CALLBACK_SCHEME)) {
            new RetrieveAccessTokenTask(this, consumer, provider, prefs).execute(uri);
            //showMessage();
        }
//      finish();
        super.onNewIntent(intent);
    }

    public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {

        private OAuthProvider provider;
        private OAuthConsumer consumer;

        public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,
                OAuthProvider provider, SharedPreferences prefs) {
            this.consumer = consumer;
            this.provider = provider;
        }

        /**
         * Retrieve the oauth_verifier, and store the oauth and
         * oauth_token_secret for future API calls.
         */
        @Override
        protected Void doInBackground(Uri... params) {
            final Uri uri = params[0];
            final String oauth_verifier = uri
                    .getQueryParameter(OAuth.OAUTH_VERIFIER);

            try {
                provider.retrieveAccessToken(consumer, oauth_verifier);

                AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
                storeAccessToken(a);

                // initialize Twitter4J
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(ConstantValues.CONSUMER_KEY, ConstantValues.CONSUMER_SECRET);
                twitter.setOAuthAccessToken(a);
                ((DogRescueApplication)getApplication()).setTwitter(twitter);

                //Log.e("Login", "Twitter Initialised");
                System.out.println("finishing...!!");
                Intent returnIntent = new Intent();
                returnIntent.putExtra("valid", "valid");
                setResult(RESULT_OK, returnIntent);
//              finish();
                Log.i(TAG, "OAuth - Access Token Retrieved");
            } catch (Exception e) {
                Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
                setResult(RESULT_CANCELED);
//              finish();
            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            System.out.println("onpostexecute...!!");
            finish();
        }

    }
    private void setConsumerProvider() {
        if (provider!=null){
            ((DogRescueApplication)getApplication()).setProvider(provider);
        }
        if (consumer!=null){
            ((DogRescueApplication)getApplication()).setConsumer(consumer);
        }
    }
    private void getConsumerProvider() {
        OAuthProvider p = ((DogRescueApplication)getApplication()).getProvider();
        if (p!=null){
            provider = p;
        }
        CommonsHttpOAuthConsumer c = ((DogRescueApplication)getApplication()).getConsumer();
        if (c!=null){
            consumer = c;
        }
    }
    private void storeAccessToken(AccessToken a) {
        System.out.println("Storing keys!!");
        SharedPreferences settings = getSharedPreferences(ConstantValues.PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("accessTokenToken", a.getToken());
        editor.putString("accessTokenSecret", a.getTokenSecret());
        editor.commit();
    }

}

我正在使用以下方法开始此活动:

give_twitter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent i = new Intent(CampaignActivity.this,PrepareRequestTokenActivity.class);
                    startActivityForResult(i, TWITTER_AUTH_REQ_CODE);
                dialog.dismiss();
            }
        });

并将响应处理为:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {

        case TWITTER_AUTH_REQ_CODE:
            if(resultCode == RESULT_OK){
                if(checkForSavedLogin()){
//                  String post_msg = "Hello.. Mobile APp";
//                  TwitterUtilities.postTweet(twitter, post_msg);
                    getTwitterProfileData();
                }
            }
            break;
        }
    }

但是onActivityResult()没有调用方法作为回报..请帮助我..

4

2 回答 2

0

你试过在 setResult(int, intent) 之后调用 finish() 吗?因为在您的情况下,它在 doInBackground 中被评论!

于 2013-07-10T12:28:34.520 回答
0

问题已解决。从网络返回后,正在再次创建活动,所以我使用了一个小条件来检查我的OnCreate. 通过使用此代码,我可以获得结果:

if (getIntent().getData()!=null) {
            Uri uri = getIntent().getData();
            if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
                String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
                try {
                    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
                    // Shared Preferences
                    Editor e = mSharedPreferences.edit();
                    e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                    e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret());
                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                    e.commit();
                    Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
                    getTwitterProfileData();

                } catch (Exception e) {
                    Log.e("Twitter Login Error", "> " + e.getMessage());
                }
            }
        }

注意:我在PrepareRequestToken课堂上进行了一些更改以匹配此内容。

于 2013-07-11T12:21:32.567 回答