blob: bf7dbdb6047a5b7fbd825eda80a31b439aec9567 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# AndroidAsync
### AndroidAsync is a java.nio based socket and http library for Android.
It uses java.nio to manage connections. All the connections are thus managed on a *single* thread, rather than one per thread.
NIO is extremely efficient.
### Download a url to a String
```java
// url is the URL to download. The callback will be invoked on the UI thread
// once the download is complete.
AsyncHttpClient.getDefaultInstance().get(url, new AsyncHttpClient.StringCallback() {
@Override
// Callback is invoked with any exceptions/errors, and the result, if available.
public void onCompleted(Exception e, String result) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("I got a string: " + result);
}
});
```
### Download a url to a file
```java
AsyncHttpClient.getDefaultInstance().get(url, filename, new AsyncHttpClient.FileCallback() {
@Override
public void onCompleted(Exception e, File result) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("my file is available at: " + result.getAbsolutePath());
}
});
```
### AndroidAsync also let's you create simple HTTP servers:
```java
// listen on port 5000
AsyncHttpServer mServer = new AsyncHttpServer(5000);
mServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send("Hello!!!");
}
});
// browsing http://localhost:5000 will return Hello!!!
```
|