Skip to content

mattleibow/AndroidAsync-binding

Repository files navigation

AndroidAsync for Xamarin.Android

Build status

Xamarin.Android binding for AndroidAsync (https://github.com/koush/AndroidAsync)

AndroidAsync is a low level network protocol library. If you're looking for a raw Socket, HTTP client/server, WebSocket, and Socket.IO library for Android, AndroidAsync is it.

Features

  • Based on NIO. One thread, driven by callbacks. Highly efficient.
  • All operations return a Future that can be cancelled
  • Socket client + socket server
  • HTTP client + server
  • WebSocket client + server

Download

Download the latest NuGet:

 PM> Install-Package AndroidAsync

Using AndroidAsync

Downloading Strings

string url = "..."; // the URL of the string to download
AsyncHttpGet get = new AsyncHttpGet(url);
AsyncHttpClient.DefaultInstance.ExecuteString(get, (ex, response, str) => {
    if (ex != null) {
        Console.WriteLine(ex);
        return;
    }

    Console.WriteLine("Downloaded a string: " + str);
});

Downloading Files

string url = "..."; // the URL of the file to download from
string filename = "..."; // the location on the device to download to
AsyncHttpGet get = new AsyncHttpGet(url);
AsyncHttpClient.DefaultInstance.GetFile(get, filename, (ex, response, file) => {
    if (ex != null) {
        Console.WriteLine(ex);
        return;
    }
    
    Console.WriteLine("Downloaded a file to: " + file.AbsolutePath);
});

Supporting Caching

// arguments are:
// 1. the http client 
// 2. the directory to store cache files 
// 3. the size of the cache in bytes
ResponseCacheMiddleware.AddCache(AsyncHttpClient.DefaultInstance,
                                 GetFileStreamPath("asynccache"),
                                 1024 * 1024 * 10);

Creating Web Sockets:

string url = "wss://echo.websocket.org"; // the web socket URL
string protocol = "my-protocol";
AsyncHttpClient.DefaultInstance.Websocket(url, protocol, (ex, webSocket) => {
    if (ex != null) {
        Console.WriteLine(ex);
        return;
    }
    
    // setting up callbacks
    webSocket.SetStringCallback((str) => {
        Console.WriteLine("Received a string: " + str);
    });

    // send data
    webSocket.Send("a string");
});

Sending Multipart/Form Data

AsyncHttpPost post = new AsyncHttpPost("http://my.server.com/postform.html");
MultipartFormDataBody body = new MultipartFormDataBody();
body.AddFilePart("my-file", new Java.IO.File("/path/to/file.txt"));
body.AddStringPart("foo", "bar");
post.Body = body;

AsyncHttpClient.DefaultInstance.ExecuteString (post, (ex, response, str) => {
    if (ex != null) {
        Console.WriteLine(ex);
        return;
    }

    Console.WriteLine("Downloaded a string: " + str);
});

Tasks (Await/Async)

All the API calls return a Task that can be awaited:

try {
    string url = "..."; // the URL of the string to download
    AsyncHttpGet get = new AsyncHttpGet(url);
    string str = await AsyncHttpClient.DefaultInstance.ExecuteStringAsync(get);

    Console.WriteLine("Downloaded a string: " + str);
} catch (Exception ex) {
    Console.WriteLine(ex);
}

Cancelling Tasks

A Task can also be cancelled using a CancellationToken:

// somewhere where everyone can access
CancellationTokenSource cts = new CancellationTokenSource();

// download something
string str = await AsyncHttpClient.DefaultInstance.ExecuteStringAsync(get, cts.Token);
Console.WriteLine("Downloaded a string: " + str);

// somewhere else
cts.Cancel();

Other Futures & Tasks

All IFuture instances have extension methods that can turn it into a Task:

IFuture future = ...;
JavaObjectType result = await future.AsTask<JavaObjectType>();

These tasks can also be allowed to cancel:

CancellationTokenSource cts = new CancellationTokenSource();

IFuture future = ...;
JavaObjectType result = await future.AsTask<JavaObjectType>(cts.Token);

About

Xamarin.Android binding for AndroidAsync (https://github.com/koush/AndroidAsync)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published