Adding custom queries [Android]


#1

Hi,

Today I needed implement a new custom class to Fabric for read the tweets of a public list because it doesn’t have native support for this call. Here are the steps to follow:

1 - Implement a new interface for make the query following the retrofit and the Twitter REST API instructions. In my case, I only need the slug of the list, the owner_screen_name and the count of tweets, but you can add all the parameters of the Twitter REST API. In this case I named it “ListService.java”:

{

    public interface ListService {
        @GET("/1.1/lists/statuses.json")
        public void statuses(@Query("slug") String slug, @Query("owner_screen_name") String owner_screen_name, @Query("count") int count, Callback<List<Tweet>> cb);
    }

2 - The next step is create a Custom Api Client that let us make a call to this Service. For this purpose we create a class that extends TwitterApiClient and we pass to the constructor an AppSession (guest session because we only need to read a public list):

{

    public class TwitterApiList extends TwitterApiClient {
    
            public TwitterApiList(AppSession session) {
                super(session);
            }
        
            public ListService getListService() {
                return getService(ListService.class);
            }
        }

3 - And for make the call when you need to get those tweets:

{

    final TwitterApiList apiClient = new TwitterApiList(guestAppSession);
    apiClient.getListService().statuses("list-slug", "owner-of-the-list", 10, new Callback<List<Tweet>>() {

        @Override
        public void success(Result<List<Tweet>> listResult) {
        final List<Tweet> tweets = listResult.data;
            // Do something with the list
        }
    
        @Override
        public void failure(TwitterException e) {
        // FAIL
        }
    });

That’s all! In the same way, we can add whatever request that there is not in the Fabric Twitter Kit.


Add custom queries [Android] [Kotlin]
#2

Hi jahertor90,

Great write up, thanks for sharing this with other users on twittercommunity!

Lien


#3

Adding new example for follow one user:

  • Using TwitterSession instead of AppSession because we need to be logged in.
  • Both interface and ApiClient in the same file (in the previous example I do in separate files for better comprehension).

Link to POST friendship/create in Twitter REST APIs

public class TwitterFollow extends TwitterApiClient {
    public TwitterFollow(TwitterSession session) {
        super(session);
    }
    public FollowService getFollowService() {
        return getService(FollowService.class);
    }

    public interface FollowService {
        @POST("/1.1/friendships/create.json")
        public void create(@Query("screen_name") String screen_name, @Query("user_id") String user_id, @Query("follow") boolean follow, Callback<User> cb);
    }
}

Use:

final TwitterFollow apiClient = new TwitterFollow(twitterSession);
apiClient.getFollowService().create("jahertor90", null, true, new Callback<User>() {
    @Override
    public void success(Result<User> result) {
        Toast.makeText(context, "Thanks for follow!" Toast.LENGTH_SHORT).show();
    }

    @Override
    public void failure(TwitterException e) {
        Toast.makeText(context, "Error following", Toast.LENGTH_SHORT).show();
    }
});

#4