Skip to content

How do you return a value from inside a ResponseHandler? #14

@ringodingoflamingo

Description

@ringodingoflamingo

Hello, I'm having fun with your Twitch API Wrapper and up untill now it has done everything that i wanted to do.

There is one question I would like to ask. I'm trying to create a "Pass me the latest highlight" functionality so my IRCBot could handle and respond to a "!latesthighlight" command.

Up until now I've got this:

public String getLatestHighlight() {
    RequestParams params = new RequestParams();
    params.put("limit", "1");
    params.put("broadcasts", "false");
    VideosResponseHandler videos = new VideosResponseHandler() {
        @Override
        public void onSuccess(int i, List<Video> list) {
            if (!list.isEmpty()) {
                // This would be the desired outcome: list.get(0).getDescription() + " - " + list.get(0).getUrl()
            }
        }

        @Override
        public void onFailure(int statusCode, String statusMessage, String errorMessage) {
            System.out.println("failure1");
        }

        @Override
        public void onFailure(Throwable e) {
            System.out.println("failure2");
        }
    };
    twitch.channels().getVideos("ringoshiba", params, videos);
    return <the string>
}

The problem is the asynchronous nature of the implementation. At the point of "return" the string would always be empty (Tried it with a 1 capacity arraylist and handling the "arraylist.add" in the onSuccess). Is there a way to let your the API return the response immediately (as in, block until there is a response)?

Regards,

Ringo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions