Example usage for twitter4j AsyncTwitterFactory getInstance

List of usage examples for twitter4j AsyncTwitterFactory getInstance

Introduction

In this page you can find the example usage for twitter4j AsyncTwitterFactory getInstance.

Prototype

public AsyncTwitter getInstance() 

Source Link

Document

Returns an instance associated with the configuration bound to this factory.

Usage

From source file:t.twitter.TTwitterModule.java

License:Open Source License

@Kroll.method
public void connect(HashMap args) {
    KrollDict arg = new KrollDict(args);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(arg.optString("apikey", ""))
            .setOAuthConsumerSecret(arg.optString("apisecret", ""))
            .setOAuthAccessToken(arg.optString("accesstoken", ""))
            .setOAuthAccessTokenSecret(arg.optString("accesssecret", ""));
    AsyncTwitterFactory tf = new AsyncTwitterFactory(cb.build());
    twitter = tf.getInstance();

    twitter.addListener(new TwitterAdapter() {
        @Override/*from   w w  w  .j a  va2s.com*/
        public void updatedStatus(Status status) {
            Log.d("Twitter", "text: " + status.getText());
        }

        @Override
        public void searched(QueryResult result) {
            HashMap<String, KrollDict[]> event = new HashMap<String, KrollDict[]>();
            List<Status> tweets = result.getTweets();
            KrollDict[] dList = new KrollDict[tweets.size()];

            // sort tweets
            Collections.sort(tweets, new Comparator<Status>() {
                public int compare(Status o1, Status o2) {

                    if (desc) {
                        if (o2.getId() < o1.getId()) {
                            return 1;
                        } else {
                            return -1;
                        }
                    } else {
                        if (o2.getId() > o1.getId()) {
                            return 1;
                        } else {
                            return -1;
                        }
                    }
                }
            });

            // return tweets to titanium
            int i = 0;
            for (Status tweet : tweets) {
                if (lastID == -1 || lastID < tweet.getId()) {
                    KrollDict d = new KrollDict();
                    d.put("username", tweet.getUser().getScreenName());
                    d.put("userimage", tweet.getUser().getProfileImageURL());
                    d.put("text", tweet.getText());
                    d.put("date", tweet.getCreatedAt());
                    d.put("id", Long.toString(tweet.getId()));
                    dList[i] = d;
                    lastID = tweet.getId();
                    i++;
                }
            }

            KrollDict[] dList2 = new KrollDict[i];

            // shorten array
            System.arraycopy(dList, 0, dList2, 0, i);

            event.put("tweets", dList2);
            success.call(getKrollObject(), event);

            synchronized (LOCK) {
                LOCK.notify();
            }
        }

        @Override
        public void verifiedCredentials(User user) {

            HashMap<String, KrollDict> event = new HashMap<String, KrollDict>();
            KrollDict d = new KrollDict();

            d.put("user_name", user.getName());
            d.put("screen_name", user.getScreenName());
            d.put("image_path", user.getProfileImageURL());
            event.put("user_info", d);
            success.call(getKrollObject(), event);
            synchronized (LOCK) {
                LOCK.notify();
            }
        }

        @Override
        public void onException(TwitterException e, TwitterMethod method) {
            synchronized (LOCK) {
                LOCK.notify();
            }
            Log.e("twitter", "error: " + e.getErrorMessage());
        }
    });
    Log.d("Twitter", "connected");
}

From source file:twitter4j.examples.async.AsyncUpdate.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.AsyncUpdate text");
        System.exit(-1);//www  .  ja v  a 2 s . c o m
    }
    AsyncTwitterFactory factory = new AsyncTwitterFactory();
    AsyncTwitter twitter = factory.getInstance();
    twitter.addListener(new TwitterAdapter() {
        @Override
        public void updatedStatus(Status status) {
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
            synchronized (LOCK) {
                LOCK.notify();
            }
        }

        @Override
        public void onException(TwitterException e, TwitterMethod method) {
            if (method == UPDATE_STATUS) {
                e.printStackTrace();
                synchronized (LOCK) {
                    LOCK.notify();
                }
            } else {
                synchronized (LOCK) {
                    LOCK.notify();
                }
                throw new AssertionError("Should not happen");
            }
        }
    });
    twitter.updateStatus(args[0]);
    synchronized (LOCK) {
        LOCK.wait();
    }
}