title: How to update tweets using Twitter 4J?
body: <p>I am using Twitter4J (2.1.0) to try to update tweets.  I can't figure out what is wrong with my code.</p>
  
  <p>In particular my problems are:</p>
  
  <p><em><strong>(a) Not all tweets post successfully. I often get the error code of -1.  According to a google group <a href="http://groups.google.com/group/twitter4j/browse_thread/thread/9beafa9dd7e2c43d?fwc=1" rel="nofollow">post</a>...</em></strong></p>
  
  <blockquote>
    <p>You get code -1 when the internal http component fails to connect to
    or read from the API.  You may also get code -1 when the API is
    unreachable from the JVM due to the DNS related issues.</p>
  </blockquote>
  
  <p>Strangely I seemed to be getting this pretty much every second post.  To deal with this whenever I received the -1 error code I would try to update again. While I realise this is not a very good solution. this fixed the probem 95% of the time </p>
  
  <p><em><strong>(b) I get a duplication errors (error code 403) whenever the new tweet matches any old tweet</em></strong></p>
  
  <p>Error code 403 occurs even if the duplicate is now outdated (eg. post "Hello there", post a variety of status updates, then post "Hello there" again throws a TwitterException with error code 403)</p>
  
  <p><em><strong>My current code...</em></strong></p>
  
  <p>My code is in an AsyncTask which is in turn in a Service (rather than activity). I have included the Asynctask code and another method below....</p>
  
  <pre><code>    class SendTwitAsyncTask extends AsyncTask&lt;String, Void, Void&gt; {
  
      @Override
      protected Void doInBackground(String... params) {
          String tokenTwit = params[0];
          String tokenSecretTwit = params[1];
          String strMessageBody = params[2];
  
          AccessToken aToken = new AccessToken(tokenTwit, tokenSecretTwit);
  
          // initialize Twitter4J
          Twitter twitter = new TwitterFactory().getInstance();
              twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
              twitter.setOAuthAccessToken(aToken);
  
  
          // create a tweet
          // strMessageBody varies
          String tweet = strMessageBody;
  
  
  
  
          boolean bool = twitter.isOAuthEnabled();
  
          if (twitter.isOAuthEnabled()) {
              GeoLocation geolocation = new GeoLocation(-33.91, 151.25);
              try {
  
                  twitter.updateStatus(tweet, geolocation);
                  showNotification("Twitter One" , TWIT_SUCCESS); 
  
              } catch (TwitterException te) {
  
                  if (te.getStatusCode() == -1) {
                      //try again
                      try {
                          twitter.updateStatus(tweet, geolocation);
  
                          showNotification("Twitter Two ", TWIT_SUCCESS);
                      } 
                      catch (TwitterException tetwo) {
                          describeTwitException(tetwo.getStatusCode());
                      }       
                  } //end if
  
                  //else exception other than -1
                  else {
                      describeTwitException(te.getStatusCode());
                  } //end else
  
              }// end outer catch
          } //end if
          else {
              showNotification("Unable to authenticate" , TWIT_FAIL);
          }//
          return null;
      }
  
  
  
  } //end class SendTwitAsyncTask
  
  public void describeTwitException(int twitExceptionCode) {
      switch (twitExceptionCode) {
  
      case (-1):
          showNotification("Twitter (unable to connect)", TWIT_FAIL);
          break;
      case(403):
          showNotification("Twitter (duplicate tweet)", TWIT_FAIL);
          break;
      default:
          showNotification("Twitter", TWIT_FAIL);
  
      } //end switch
  }  //end describeTwitException method 
  </code></pre>