0
votes

In twitter how can we fetch the number of followers.I have gone through twitter developer from where I got the url to fetch the followers list. URL is :GET https://api.twitter.com/1.1/followers/list.json?cursor=-1&screen_name=twitterdev&skip_status=true&include_user_entities=false I am not getting how can we fetch the response in Android.

1

1 Answers

3
votes
   private void getFollowerList()
{
    if (!Utils.isInternetOn(InviteFriendActivity.this)){
        Utils.showToast(this,getString(R.string.no_internet));
        return;
    }
    progressDialog.show();
    final TwitterSession session = Twitter.getSessionManager()
            .getActiveSession();
    TwitterAuthToken authToken = session.getAuthToken();
    String token = authToken.token;
    String secret = authToken.secret;

//Here we get all the details of user's twitter account
        Call<com.twitter.sdk.android.core.models.User> userCall= Twitter.getApiClient(session).getAccountService()
                .verifyCredentials(true, false);
        userCall.enqueue(new Callback<com.twitter.sdk.android.core.models.User>() {
            @Override
            public void onResponse(Call<com.twitter.sdk.android.core.models.User> call, Response<com.twitter.sdk.android.core.models.User> response) {
                MyTwitterApiClient myTwitterApiClient = new MyTwitterApiClient(session);
                myTwitterApiClient.getCustomService().list(response.body().id).enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        Log.e("list",response.body().toString());
                        BufferedReader reader = null;
                        StringBuilder sb = new StringBuilder();

                        reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));

                        String line;

                        try {
                            while ((line = reader.readLine()) != null) {
                                sb.append(line);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            progressDialog.dismiss();
                        }


                        String result = sb.toString();
                        System.out.println("Response is>>>>>>>>>"+result);
                        JsonObject obj= new Gson().fromJson(result,JsonObject.class);
                        JsonArray usersArray= (JsonArray) obj.get("users");
                        JsonArray userIds = new JsonArray();
                        for(int i=0;i<usersArray.size();i++){
                            JsonObject userObject = (JsonObject) usersArray.get(i);
                            userIds.add(userObject.get("id_str"));
                        }
                        // Api call with Twitter followers ids
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.add("twitter_ids",userIds);

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Utils.showToast(InviteFriendActivity.this,getString(R.string.something_went_wrong));
                        progressDialog.dismiss();
                    }

                });
            }

            @Override
            public void onFailure(Call<com.twitter.sdk.android.core.models.User> call, Throwable t) {
                Utils.showToast(InviteFriendActivity.this,getString(R.string.something_went_wrong));
                progressDialog.dismiss();
            }
        });
    }

this is function to be called to get followers list.

public class MyTwitterApiClient extends TwitterApiClient {
    public MyTwitterApiClient(TwitterSession session) {
        super(session);
    }

    /**
     * Provide CustomService with defined endpoints
     */
    public Api getCustomService() {
        return getService(Api.class);
    }
}

this is ApiCLientClass

in this example only ids are stored in onResponse method usersArray will contain all userList with all available data of user. you can remove and perform your check for internet connection and showing toast which i've done in Utils class. And the Api class is this

    public interface Api {
 @GET("/1.1/followers/list.json")
    Call<ResponseBody> list(@Query("user_id") long id);
}