Example usage for twitter4j Twitter getFriendsList

List of usage examples for twitter4j Twitter getFriendsList

Introduction

In this page you can find the example usage for twitter4j Twitter getFriendsList.

Prototype

PagableResponseList<User> getFriendsList(long userId, long cursor, int count) throws TwitterException;

Source Link

Document

Returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").
At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues.

Usage

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.ProfileFragment.java

License:Apache License

public void getFollowing(final User user, final AsyncListView listView) {
    spinner.setVisibility(View.VISIBLE);
    canRefresh = false;// ww  w  .  j a  va  2  s  .  c om

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                PagableResponseList<User> friendsPaging;
                try {
                    friendsPaging = twitter.getFriendsList(user.getId(), currentFollowing, 100);
                } catch (OutOfMemoryError e) {
                    return;
                }

                for (int i = 0; i < friendsPaging.size(); i++) {
                    following.add(friendsPaging.get(i));
                    Log.v("friends_list", friendsPaging.get(i).getName());
                }

                if (friendsPaging.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                currentFollowing = friendsPaging.getNextCursor();

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (followingAdapter == null) {
                            followingAdapter = new PeopleArrayAdapter(context, following);
                            listView.setAdapter(followingAdapter);
                        } else {
                            followingAdapter.notifyDataSetChanged();
                        }

                        if (settings.roundContactImages) {
                            ImageUtils.loadSizedCircleImage(context, profilePicture,
                                    thisUser.getOriginalProfileImageURL(), mCache, 96);
                        } else {
                            ImageUtils.loadImage(context, profilePicture, thisUser.getOriginalProfileImageURL(),
                                    mCache);
                        }

                        String url = user.getProfileBannerURL();
                        ImageUtils.loadImage(context, background, url, mCache);

                        canRefresh = true;
                        spinner.setVisibility(View.GONE);
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (user != null && user.isProtected()) {
                                Toast.makeText(context, getResources().getString(R.string.protected_account),
                                        Toast.LENGTH_SHORT).show();
                                if (settings.roundContactImages) {
                                    ImageUtils.loadSizedCircleImage(context, profilePicture,
                                            thisUser.getOriginalProfileImageURL(), mCache, 96);
                                } else {
                                    ImageUtils.loadImage(context, profilePicture,
                                            user.getOriginalProfileImageURL(), mCache);
                                }

                                String url = user.getProfileBannerURL();
                                ImageUtils.loadImage(context, background, url, mCache);
                            } else {
                                Toast.makeText(context,
                                        getResources().getString(R.string.error_loading_timeline),
                                        Toast.LENGTH_SHORT).show();
                            }
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // fragment not attached
                        }
                        canRefresh = false;
                        hasMore = false;
                    }
                });
            }
        }
    }).start();
}

From source file:ontoSentiment.Amigos.java

@Override
public void run() {
    ConfigurationBuilder cb = new ConfigurationBuilder();

    //the following is set without accesstoken- desktop client
    cb.setDebugEnabled(true).setOAuthConsumerKey("FBd5n7dyl8mCz73qyfZ0p4XHb")
            .setOAuthConsumerSecret("vu5Xt5TzBSL9naOZylIYlx5MdcRlhH2LvkpW6KIkxSf9AqwuGt")
            .setOAuthAccessToken("3232400175-lAchtC6ChWMTnJKe3BaWbst8SucIaTjn5gm4Rp2")
            .setOAuthAccessTokenSecret("DnkquBWAS6igYpM8Z4r54hH7ztcfMX6u8OzMXBLwM9Xkh");

    try {/*from w  w w.ja  va 2  s.  co  m*/
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        //User u = twitter.showUser("karlaffabiola");            
        User u = twitter.showUser("raythemaster");
        IDs ids;
        System.out.println("Listing followers's ids.");

        System.out.println("ID: " + u.getId());
        System.out.println("Nome: " + u.getScreenName());

        long cursor = -1;
        PagableResponseList<User> pagableFollowings;
        List<User> listFriends = new ArrayList<>();
        List<User> listFriends2 = new ArrayList<>();

        pagableFollowings = twitter.getFriendsList(u.getId(), cursor, 200);
        System.out.println("Qunatidade followers: " + pagableFollowings.size());
        for (User user : pagableFollowings) {
            System.out.println("Id: " + user.getId() + " Nome: " + user.getScreenName());
            listFriends.add(user); // ArrayList<User>
        }

        for (User user : listFriends) {
            System.out.println("Id1: " + user.getId() + " Nome1: " + user.getScreenName());
            pagableFollowings = twitter.getFriendsList(user.getId(), cursor, 200);
            System.out.println("Qunatidade followers: " + pagableFollowings.size());
            for (User user2 : pagableFollowings) {
                System.out.println("Id2: " + user2.getId() + " Nome2: " + user2.getScreenName());
                listFriends2.add(user2); // ArrayList<User>
            }
        }
        System.out.println("Lista 1:" + listFriends.size());
        System.out.println("Lista 2:" + listFriends2.size());
        System.exit(0);
    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get timeline: " + new Date());
        try {
            Thread.sleep(3 * 60 * 1000);
            run();
        } catch (InterruptedException ex) {
            Logger.getLogger(Amigos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}