2013-08-26 65 views
0

我正在使用編輯文本框和發佈按鈕在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(""); 
       } 
      }); 
     }} 
    } 
+0

你硬編碼在postexecute敬酒。這並不能保證推文成功。看看你是否在doInbackground方法中得到了任何異常 –

+0

它給出了這個logcat結果 08-26 03:09:46.631:D/Twitter更新錯誤(2899):400:請求無效。隨附的錯誤信息將解釋原因。這是狀態碼將在版本1.0速率限制(https://dev.twitter.com/pages/rate-limiting)期間返回。在API v1.1中,沒有認證的請求被認爲是無效的,你會得到這個響應。 D/Twitter更新錯誤(2899):消息 - 錯誤的身份驗證數據 D/Twitter更新錯誤(2899):代碼 - 215 – Nidhi

回答

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(); 
       } 
      } 
     }); 
    } 
} 
相關問題