发布推文不会在Twitter时间轴上显示

时间:2013-08-26 06:56:08

标签: android twitter

我正在使用编辑文本框和帖子按钮,使用我的应用在Twitter的时间轴上发布推文。

但是在完成所有处理后,“状态更新成功”吐司出现了,但是推文没有在时间轴上显示...它在LogCat中给出错误400并显示有关错误认证数据的消息!!我是OAuth的新手。我该如何解决这个问题?

以下是代码:

    public class PostCommentActivity extends Activity {

        private static final String CONSUMER_KEY = "*****";
        private static final String CONSUMER_SECRET = "*******";
        static String PREFERENCE_NAME = "twitter_oauth";
        static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
        static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
        static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";

        SharedPreferences mSharedPreferences;
        ProgressDialog pDialog;
        EditText et;
        Button postbtn;
        Twitter twitter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            setContentView(R.layout.post_comment);
            super.onCreate(savedInstanceState);
            et = (EditText) findViewById(R.id.editText);

            mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref",
                                 0);
            postbtn = (Button) findViewById(R.id.Post_btn);
            postbtn.setOnClickListener(new OnClickListener() {@Override

            public void onClick(View v) {
                // Get the status from EditText
                Twitter twitter = TwitterFactory.getSingleton();
                String status = et.getText().toString();

                // Check for blank text
                if (status.trim().length() > 0) {
                    // update status
                    new updateTwitterStatus().execute(status);
                }
                else {
                    // EditText is empty
                    Toast.makeText(getApplicationContext(),
                                "Please enter status message", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    class updateTwitterStatus extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(PostCommentActivity.this);
            pDialog.setMessage("Updating to twitter...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * Getting Places JSON
         *
         **/
        protected String doInBackground(String... args) {
            Log.d("Tweet Text", "> " + args[0]);
            String status = args[0];
            try {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.setOAuthConsumerKey(CONSUMER_KEY);
                builder.setOAuthConsumerSecret(CONSUMER_SECRET);

                ;    // Access Token
                String access_token = mSharedPreferences.getString(
                        PREF_KEY_OAUTH_TOKEN, "");

                // Access Token Secret
                String access_token_secret = mSharedPreferences.getString(
                                               PREF_KEY_OAUTH_SECRET, "");
                AccessToken accessToken = new AccessToken(access_token,
                access_token_secret);
                Twitter twitter = new TwitterFactory(builder.build())
                                        .getInstance(accessToken);
                twitter.setOAuthAccessToken(accessToken);

                // Update status
                twitter4j.Status response = twitter.updateStatus(status);
                Log.d("Status", "> " + response.getText());
            }
            catch (TwitterException e) {
                // Error in updating status
                Log.d("Twitter Update Error", e.getMessage());
            }
            return null;
        }
        protected void onPostExecute(String file_url) {

            // Dismiss the dialog after getting all products
            pDialog.dismiss();

            // Updating UI from Background Thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Status tweeted successfully", Toast.LENGTH_SHORT).show();

                    // Clearing EditText field
                    et.setText("");
                }
             });
        }}
    }

1 个答案:

答案 0 :(得分:1)

我得到了解决方案......试试这段代码:

public class PostCommentActivity extends Activity {

    private static final String CONSUMER_KEY = "***";
    private static final String CONSUMER_SECRET = "******";

    SharedPreferences mSharedPreferences;

    EditText et;
    Button postbtn;
    RequestToken requestToken;
    Twitter twitter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        setContentView(R.layout.post_comment);
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        et = (EditText) findViewById(R.id.editText);
        mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref",
                             0);

        postbtn = (Button) findViewById(R.id.Post_btn);
        postbtn.setOnClickListener(new OnClickListener() {    @Override
            public void onClick(View v) {

                String token = "*****";
                String secret = "*****";
                AccessToken a = new AccessToken(token, secret);
                Twitter twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                twitter.setOAuthAccessToken(a);
                String status = et.getText().toString();
                if ((status.trim().length() > 0)) {
                    try {
                        twitter.updateStatus(status);

                        Toast.makeText(getApplicationContext(),
                                "Status tweeted successfully", Toast.LENGTH_SHORT).show();
                        // Clearing EditText field
                        et.setText("");
                    }
                    catch (TwitterException e) {

                        e.printStackTrace();
                    }
                }
                else {
                    // EditText is empty
                    Toast.makeText(getApplicationContext(),
                            "Please enter status message", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}