Skip to content

Repository files navigation

AndroidAsync

AndroidAsync is a low level network protocol library. If you are looking for an easy to use, higher level, Android aware, http request library, check out Ion (it is built on top of AndroidAsync). The typical Android app developer would probably be more interested in Ion.

But 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
  • Socket.IO client

Download

Download the latest JAR or grab via Maven:

<dependency>
<groupId>com.koushikdutta.async</groupId>
<artifactId>androidasync</artifactId>
<version>(insert latest version)</version>
</dependency>

Download a url to a String

// url is the URL to download. The callback will be invoked on the UI thread// once the download is complete.AsyncHttpClient.getDefaultInstance().getString(url, newAsyncHttpClient.StringCallback() {
// Callback is invoked with any exceptions/errors, and the result, if available.@OverridepublicvoidonCompleted(Exceptione, AsyncHttpResponseresponse, Stringresult) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("I got a string: " + result);
}
});

Download JSON from a url

// url is the URL to download. The callback will be invoked on the UI thread// once the download is complete.AsyncHttpClient.getDefaultInstance().getJSONObject(url, newAsyncHttpClient.JSONObjectCallback() {
// Callback is invoked with any exceptions/errors, and the result, if available.@OverridepublicvoidonCompleted(Exceptione, AsyncHttpResponseresponse, JSONObjectresult) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("I got a JSONObject: " + result);
}
});

Download a url to a file

AsyncHttpClient.getDefaultInstance().getFile(url, filename, newAsyncHttpClient.FileCallback() {
@OverridepublicvoidonCompleted(Exceptione, AsyncHttpResponseresponse, Fileresult) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("my file is available at: " + result.getAbsolutePath());
}
});

Caching is supported too

// arguments are the http client, the directory to store cache files, and the size of the cache in bytesResponseCacheMiddleware.addCache(AsyncHttpClient.getDefaultInstance(),
getFileStreamPath("asynccache"),
1024 * 1024 * 10);

Can also create web sockets:

AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", newWebSocketConnectCallback() {
@OverridepublicvoidonCompleted(Exceptionex, WebSocketwebSocket) {
if (ex != null) {
ex.printStackTrace();
return;
}
webSocket.send("a string");
webSocket.send(newbyte[10]);
webSocket.setStringCallback(newStringCallback() {
publicvoidonStringAvailable(Strings) {
System.out.println("I got a string: " + s);
}
});
webSocket.setDataCallback(newDataCallback() {
publicvoidonDataAvailable(ByteBufferListbyteBufferList) {
System.out.println("I got some bytes!");
// note that this data has been readbyteBufferList.clear();
}
});
}
});

AndroidAsync also supports socket.io

SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://192.168.1.2:3000", newSocketIOConnectCallback() {
@OverridepublicvoidonConnectCompleted(Exceptionex, SocketIOClientclient) {
if (ex != null) {
ex.printStackTrace();
return;
}
client.setStringCallback(newStringCallback() {
@OverridepublicvoidonString(Stringstring) {
System.out.println(string);
}
});
client.setEventCallback(newEventCallback() {
@OverridepublicvoidonEvent(Stringevent, JSONArrayarguments) {
System.out.println("event: " + event + " args: " + arguments.toString());
}
});
client.setJSONCallback(newJSONCallback() {
@OverridepublicvoidonJSON(JSONObjectjson) {
System.out.println("json: " + json.toString());
}
});
}
});

Need to do multipart/form-data uploads? That works too.

AsyncHttpPostpost = newAsyncHttpPost("http://myservercom/postform.html");
MultipartFormDataBodybody = newMultipartFormDataBody();
body.addFilePart("my-file", newFile("/path/to/file.txt");
body.addStringPart("foo", "bar");
post.setBody(body);
AsyncHttpClient.getDefaultInstance().execute(post, newStringCallback() {
@OverridepublicvoidonCompleted(Exceptione, AsyncHttpResponsesource, Stringresult) {
if (e != null) {
ex.printStackTrace();
return;
}
System.out.println("Server says: " + result);
}
});

AndroidAsync also let's you create simple HTTP servers (and websocket servers):

AsyncHttpServerserver = newAsyncHttpServer();
server.get("/", newHttpServerRequestCallback() {
@OverridepublicvoidonRequest(AsyncHttpServerRequestrequest, AsyncHttpServerResponseresponse) {
response.send("Hello!!!");
}
});
// listen on port 5000server.listen(5000);
// browsing http://localhost:5000 will return Hello!!!

Futures

All the API calls return Futures.

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

Futures can also have callbacks...

Future<String> string = client.getString("http://foo.com/hello.txt");
string.setCallback(newFutureCallback<String>() {
@OverridepublicvoidonCompleted(Exceptione, Stringresult) {
System.out.println(result);
}
});

For brevity...

client.getString("http://foo.com/hello.txt")
.setCallback(newFutureCallback<String>() {
@OverridepublicvoidonCompleted(Exceptione, Stringresult) {
System.out.println(result);
}
});

About

Asynchronous socket, http (client+server), websocket, and socket.io library for android. Based on nio, not threads.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages