Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (49 sloc) 1.7 KB

{{{ "title" : "Futures in AndroidAsync", "date": "5-20-2013" }}}

If you haven't used AndroidAsync yet, you should. It's a powerful NIO based socket and HTTP library for Android. Super fast, easy to use, and asynchronous.

The initial implementation was entirely callback based. So, you'd do something like, this:

client.getString(url, new AsyncHttpClient.StringCallback() {
    // Callback is invoked with any exceptions/errors, and the result, if available.
    @Override
    public void onCompleted(Exception e, AsyncHttpResponse response, String result) {
        if (e != null) {
            e.printStackTrace();
            return;
        }
        System.out.println("I got a string: " + result);
    }
});

I got a few request for Futures, which are also a nice pattern. So I added that transparently by making all the asynchronous calls return a Future instead of void.

So, now:

Future<String> string = client.getString("http://foo.com/hello.txt");
// this will block, and may also throw if there was an error!
String value = string.get();

Futures can also have callbacks...

Future<String> string = client.getString("http://foo.com/hello.txt");
string.setCallback(new FutureCallback<String>() {
    @Override
    public void onCompleted(Exception e, String result) {
        System.out.println(result);
    }
});

For brevity...

client.getString("http://foo.com/hello.txt")
.setCallback(new FutureCallback<String>() {
    @Override
    public void onCompleted(Exception e, String result) {
        System.out.println(result);
    }
});

Enjoy!