2014-01-30 2 views
0

события кнопки здесь:Сообщения не загружая при нажатии кнопки

btnUpdateStatus.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Call update status function 
       // Get the status from EditText 
       String status = txtUpdate.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(); 
       } 
      } 
     }); 

здесь обновление dmethod класса updateTwitterStatus расширяет AsyncTask {

/** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.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(TWITTER_CONSUMER_KEY); 
      builder.setOAuthConsumerSecret(TWITTER_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); 

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

    /** 
    * After completing background task Dismiss the progress dialog and show 
    * the data in UI Always use runOnUiThread(new Runnable()) to update UI 
    * from background thread, otherwise you will get error 
    * **/ 
    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 
       txtUpdate.setText(""); 
      } 
     }); 
    } 

} 

Вот XML макет:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <!-- Twitter Login Button --> 
    <Button android:id="@+id/btnLoginTwitter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Login with Twitter" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="30dip"/> 

    <!-- user name label --> 
    <TextView android:id="@+id/lblUserName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_marginTop="30dip"/> 

    <!-- label update status --> 
    <TextView android:id="@+id/lblUpdate" 
     android:text="Update Status" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:visibility="visible"/> 

    <!-- Tweet EditText --> 
    <EditText android:id="@+id/txtUpdateStatus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dip" 
     android:visibility="visible"/> 

    <!-- Tweet Button --> 
    <Button android:id="@+id/btnUpdateStatus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Tweet" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:visibility="visible"/> 

    <!-- Twitter Logout button --> 
    <Button android:id="@+id/btnLogoutTwitter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Logout from Twitter" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="50dip" 
     android:visibility="visible"/> 



</LinearLayout> 

Я использую twitter SDK и twiiter 4jcore для отправки сообщений и входа в систему с твиттером. Я получаю успешный логин, но когда я пишу любое сообщение для обновления на Twitter, мое сообщение не отображается в моей учетной записи Twitter. Что я делаю неправильно? Я следовал этому руководству here, но мое сообщение не отображается.

+0

Значит, вы не могли видеть свои твиты в Twitter ??? – Piyush

+0

@PiyushGupta да, сэр .., я не получаю никакого сообщения, которое я пишу с моего устройства или с моего эмулятора. – rajshree

+0

Проверьте свой код для сообщений твитов в Twitter – Piyush

ответ

1

Следуйте по ссылке Demo twitter post with oath. Проверьте сообщение учебника на твиттер. Если вы все еще сталкиваетесь с проблемами, дайте мне знать.

Смежные вопросы