Example usage for twitter4j TwitterException getMessage

List of usage examples for twitter4j TwitterException getMessage

Introduction

In this page you can find the example usage for twitter4j TwitterException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:alberapps.java.noticias.tw.ProcesarTwitter.java

License:Open Source License

/**
 * listado alberapps/*from ww w  . ja v  a 2s.com*/
 *
 * @return listado
 */
public static List<TwResultado> procesarAlberApps() {

    List<TwResultado> lista = new ArrayList<>();

    try {
        List<TwResultado> listaInicial;

        // Iniciar
        ProcesarTwitter4j procesar4j = new ProcesarTwitter4j();
        procesar4j.setUp();

        // Tram
        listaInicial = procesar4j.recuperarTimeline("alberapps", tw_alberapps_ruta, 15);

        // Eliminar las que sean de conversacion y retweet
        for (int i = 0; i < listaInicial.size(); i++) {

            if (listaInicial.get(i).getRespuestaId() == -1 && !listaInicial.get(i).isRetweet()) {
                lista.add(listaInicial.get(i));
            }

        }

        if (lista.size() < 2) {

            lista.clear();

            listaInicial = procesar4j.recuperarTimeline("alberapps", tw_alberapps_ruta, 25);

            // Eliminar las que sean de conversacion y retweet
            for (int i = 0; i < listaInicial.size(); i++) {

                if (listaInicial.get(i).getRespuestaId() == -1 && !listaInicial.get(i).isRetweet()) {
                    lista.add(listaInicial.get(i));
                }

            }

        }

        if (lista != null && !lista.isEmpty()) {

            // Ordenar por fecha
            Collections.sort(lista);

        } else {

            return null;

        }

    } catch (TwitterException e) {
        //88 rate limit exceeded
        TwResultado resultado = new TwResultado();

        resultado.setError(Integer.toString(e.getErrorCode()));
        resultado.setMensajeError(e.getErrorMessage());

        lista = new ArrayList<>();

        lista.add(resultado);

        e.printStackTrace();
    } catch (Exception e) {

        TwResultado resultado = new TwResultado();

        resultado.setError("100");
        resultado.setMensajeError(e.getMessage());

        lista = new ArrayList<>();

        lista.add(resultado);

        e.printStackTrace();

    }

    return lista;

}

From source file:android.stickynotes.StickyNotesActivity.java

License:Apache License

private void getTweets(String twit) {
    wifi.setWifiEnabled(true);//  w w w  .jav  a  2 s .  co m
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("TvywVhWx7r7QQev2UGfA4g")
            .setOAuthConsumerSecret("Nv22zsyf1VS0vvi6hwAMyvJk9LUtSXwRUB4xwp2gRs");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    try {
        QueryResult result = twitter.search(new Query(twit));
        List<Tweet> tweets = result.getTweets();
        textStatus.setText("");
        textStatus.append("Recent tweets about '" + twit + "':\n");
        for (Tweet tweet : tweets) {
            textStatus.append("@" + tweet.getFromUser() + " - " + tweet.getText() + "\n\n");
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        textStatus.append("Failed to search tweets: " + te.getMessage() + " " + twit);
    }
}

From source file:at.illecker.storm.commons.util.io.JsonUtils.java

License:Apache License

public static List<Status> readTweets(InputStream tweetsFile, String filterLanguage) {
    List<Status> tweets = new ArrayList<Status>();
    InputStreamReader isr = null;
    BufferedReader br = null;//from w  w  w.  jav  a  2s . c o m
    try {
        isr = new InputStreamReader(tweetsFile, "UTF-8");
        br = new BufferedReader(isr);
        String rawJSON = "";
        while ((rawJSON = br.readLine()) != null) {
            // rawJSON may include multiple status objects within one line
            String regex = "\"created_at\":";
            String[] rawJSONTweets = rawJSON.split("\\}\\{" + regex);

            if (rawJSONTweets.length == 0) { // only one object
                try {
                    Status status = TwitterObjectFactory.createStatus(rawJSON);
                    tweets.add(status);
                    // LOG.info("@" + status.getUser().getScreenName() + " - "
                    // + status.getText());
                } catch (TwitterException twe) {
                    LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                }

            } else { // read multiple objects
                for (int j = 0; j < rawJSONTweets.length; j++) {
                    if (j == 0) {
                        rawJSONTweets[j] = rawJSONTweets[j] + "}";
                    } else if (j == rawJSONTweets.length) {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j];
                    } else {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j] + "}";
                    }
                    try {
                        Status status = TwitterObjectFactory.createStatus(rawJSONTweets[j]);
                        if (filterLanguage != null) {
                            if (status.getLang().equals(filterLanguage)) {
                                tweets.add(status);
                            }
                        } else {
                            tweets.add(status);
                        }
                        // LOG.info("@" + status.getUser().getScreenName() + " - "
                        // + status.getText());
                    } catch (TwitterException twe) {
                        LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ignore) {
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ignore) {
            }
        }
    }
    LOG.info("Loaded " + tweets.size() + " tweets");
    return tweets;
}

From source file:au.com.infiniterecursion.hashqanda.MainActivity.java

public boolean loadTweets() {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Q2DfeCNOxprbDp66fWlw")
            .setOAuthConsumerSecret("TgyJ26CKXz1SxGiEJx4HG9rytFsqiMZlEPeCO7S9g");

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    // clear tweet list.
    MainActivity.this.runOnUiThread(new Runnable() {

        public void run() {
            // add to it , via the runOnUIThread
            app.getTweetList().clear();/*from   w w w.  j  a v  a 2  s .co  m*/
        }

    });

    try {
        Log.d(TAG, " starting Tweets loading ");
        Query q = new Query("#qanda");

        q.setRpp(numberTweets);

        QueryResult result = twitter.search(q);
        List<Tweet> tweets = result.getTweets();
        for (Tweet tweet : tweets) {
            // Log.d(TAG, "@" + tweet.getFromUser() + " - " +
            // tweet.getText());
            // Log.d(TAG, " img url " + tweet.getProfileImageUrl());

            final CachedBitmapAndTweet cachedbmtwt = new CachedBitmapAndTweet();

            cachedbmtwt.twt = tweet;

            try {
                URL url = new URL(tweet.getProfileImageUrl());
                InputStream is = (InputStream) url.getContent();

                if (is != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    cachedbmtwt.bm = bitmap;
                } else {
                    cachedbmtwt.bm = null;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
                cachedbmtwt.bm = null;
            } catch (IOException e) {
                e.printStackTrace();
                cachedbmtwt.bm = null;
            } catch (NullPointerException npe) {
                npe.printStackTrace();
                cachedbmtwt.bm = null;
            }

            MainActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                    // add to it , via the runOnUIThread
                    app.getTweetList().add(cachedbmtwt);
                }

            });

        }

        Log.d(TAG, " finished Tweets loading ");

        return true;

    } catch (TwitterException te) {
        te.printStackTrace();
        Log.d(TAG, "Failed to search tweets: " + te.getMessage());
        return false;
    }
}

From source file:au.com.infiniterecursion.hashqanda.MainActivity.java

private void showTweetDialog() {

    app.setShowing_tweet_dialog(true);/*w ww.j  a  va  2s  . c  om*/

    // Launch Tweet Edit View
    LayoutInflater inflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final View tweet_dialog_view = inflater.inflate(R.layout.tweet_dialog, null);

    final EditText tweet_edittext = (EditText) tweet_dialog_view.findViewById(R.id.EditTextTweet);

    final Dialog d = new Dialog(this);
    Window w = d.getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    d.setTitle(R.string.tweet_dialog_title);
    // the edit layout defined in an xml file (in res/layout)
    d.setContentView(tweet_dialog_view);
    // Cancel
    Button cbutton = (Button) d.findViewById(R.id.button2Cancel);
    cbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            app.setShowing_tweet_dialog(false);
            d.dismiss();
        }
    });
    // Edit
    Button ebutton = (Button) d.findViewById(R.id.button1Edit);
    ebutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // save title and description to DB.
            String tweet = tweet_edittext.getText().toString();

            int end = (tweet.length() > 133) ? 133 : tweet.length();
            String real_tweet = "#qanda " + tweet.substring(0, end);
            Log.d(TAG, "New tweet is " + tweet);
            Log.d(TAG, "real tweet is " + real_tweet);

            String twitterToken, twitterTokenSecret;
            String[] toks = twitterTokens();
            twitterToken = toks[0];
            twitterTokenSecret = toks[1];

            if (twitterToken != null && twitterTokenSecret != null) {

                // Ok, now we can tweet this URL
                AccessToken a = new AccessToken(twitterToken, twitterTokenSecret);
                Twitter twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(TwitterOAuthActivity.consumerKey, TwitterOAuthActivity.consumerSecret);
                twitter.setOAuthAccessToken(a);

                try {
                    twitter.updateStatus(real_tweet);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(MainActivity.this, R.string.tweet_posted, Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (TwitterException e) {
                    // 
                    e.printStackTrace();
                    Log.e(TAG, " twittering failed " + e.getMessage());
                }
            } else {

                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(MainActivity.this, R.string.tweet_not_posted, Toast.LENGTH_LONG).show();
                    }
                });

            }

            app.setShowing_tweet_dialog(false);
            d.dismiss();
        }
    });
    d.show();
}

From source file:au.com.infiniterecursion.vidiom.activity.MainActivity.java

public void finishedUploading(boolean success) {
    // not uploading anymore.

    // Auto twittering.
    ////from w ww.  j a v a 2s.co m
    if (twitterPreference && success) {

        new Thread(new Runnable() {
            public void run() {

                // Check there is a hosted URL for a start..

                // XXX get which URL from user if more than one!!

                String[] hosted_urls_to_tweet = db_utils
                        .getHostedURLsFromID(new String[] { Long.toString(latestsdrecord_id) });

                Log.d(TAG, " checking " + hosted_urls_to_tweet[0] + " in auto twitter publishing");

                if (hosted_urls_to_tweet != null && hosted_urls_to_tweet[0] != null
                        && hosted_urls_to_tweet[0].length() > 0) {

                    // Check there is a valid twitter OAuth tokens.
                    String twitterToken = prefs.getString("twitterToken", null);
                    String twitterTokenSecret = prefs.getString("twitterTokenSecret", null);

                    if (twitterToken != null && twitterTokenSecret != null) {

                        // Ok, now we can tweet this URL
                        AccessToken a = new AccessToken(twitterToken, twitterTokenSecret);
                        Twitter twitter = new TwitterFactory().getInstance();
                        twitter.setOAuthConsumer(TwitterOAuthActivity.consumerKey,
                                TwitterOAuthActivity.consumerSecret);
                        twitter.setOAuthAccessToken(a);

                        String status = "New video:" + hosted_urls_to_tweet[0];
                        try {
                            twitter.updateStatus(status);
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    Toast.makeText(MainActivity.this, R.string.tweeted_ok, Toast.LENGTH_LONG)
                                            .show();
                                }
                            });
                        } catch (TwitterException e) {
                            //
                            e.printStackTrace();
                            Log.e(TAG, "Auto twittering failed " + e.getMessage());
                        }
                    }

                }

            }
        }).start();

    }

    mainapp.setNotUploading();
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Get the screen name of the Twitter user (eg KRuddPM)
 * @param userToken/*from  w ww  . j av a 2  s .c o  m*/
 * @param userSecret
 * @return the screen name, or null
 */
public String getScreenName(String userToken, String userSecret) {
    Twitter twitter = getTwitterAuthForUser(userToken, userSecret);
    if (twitter == null) {
        return null;
    }

    try {
        return twitter.verifyCredentials().getScreenName();

    } catch (TwitterException e) {
        log.error("Error getting screen name: " + e.getClass() + ": " + e.getMessage());
    }
    return null;
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Get a RequestToken for this Twitter application. This token MUST be captured so that it can 
 * be used for future requests in the same process.
 * @return RequestToken or null if error
 *//*from  www. ja  va 2  s  .co m*/
public RequestToken getRequestToken() {
    if (twitterConsumer == null) {
        log.error("No OAuth instance");
        return null;
    }

    try {
        return twitterConsumer.getOAuthRequestToken();

    } catch (TwitterException e) {
        log.error("Error getting RequestToken: " + e.getClass() + ": " + e.getMessage());
    }

    return null;
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Get an AccessToken for the given RequestToken and user entered pin.
 * @param requestToken/*from   ww  w. j  a va 2s  .  c om*/
 * @param pin
 * @return AccessToken or null if error
 */
public AccessToken getAccessToken(RequestToken requestToken, String pin) {
    if (twitterConsumer == null) {
        log.error("No OAuth instance");
        return null;
    }

    try {
        return twitterConsumer.getOAuthAccessToken(requestToken, pin);

    } catch (TwitterException e) {
        log.error("Error getting AccessToken: " + e.getClass() + ": " + e.getMessage());
    }

    return null;
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Validate that twitter consumer is not null (i.e. key and secret are not null)
 * @return false if error//  w w  w.  jav a2 s  .co m
 */
public boolean validate() {
    if (twitterConsumer == null) {
        log.error("No OAuth instance");
        return false;
    }

    try {
        // TODO: it seems that this method does not verify consumer key and secret (Denny)
        return twitterConsumer.test();
    } catch (TwitterException e) {
        log.error("Error in testing API: " + e.getClass() + ": " + e.getMessage());
    }

    return false;
}