0

我正在使用编辑文本框和发布按钮使用我的应用在 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("");
                }
             });
        }}
    }
4

1 回答 1

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();
                }
            }
        });
    }
}
于 2013-08-27T04:51:56.163 回答