【问题标题】:Posted tweet doesn't show on Twitter timeline发布的推文未显示在 Twitter 时间线上
【发布时间】:2015-10-23 06:37:35
【问题描述】:

我正在使用编辑文本框和发布按钮通过我的应用在 Twitter 的时间线上发布推文。

但在所有处理之后出现“状态更新成功”toast,但时间轴上没有显示推文...它在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("");
                }
             });
        }}
    }

【问题讨论】:

  • 您已经在 postexecute 中硬编码了 toast。这并不能保证推文是成功的。看看你是否得到任何你在 doInbackground 方法中捕获的异常
  • 它给出了这个 logcat 结果 08-26 03:09:46.631:D/Twitter 更新错误(2899):400:请求无效。随附的错误消息将解释原因。这是版本 1.0 速率限制期间将返回的状态代码 (dev.twitter.com/pages/rate-limiting)。在 API v1.1 中,未经身份验证的请求被视为无效,您将收到此响应。 D/Twitter 更新错误(2899):消息 - 错误的身份验证数据 D/Twitter 更新错误(2899):代码 - 215

标签: android twitter


【解决方案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();
                }
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 2018-12-07
    • 2013-04-23
    • 1970-01-01
    • 2013-07-10
    • 2014-03-27
    相关资源
    最近更新 更多