AndroidAsync
AndroidAsync是一个底层网络协议库。如果你正在寻求一个便于使用,高级的,适配Android的,http请求的库,可以查看Ion。
特性
基于NIO。一个县城,通过回调驱动。高效率。?
所有的操作返回一个可以取消的Future。
Socket客户端+套接字服务
HTTP客户端+服务
WebSocket客户端+服务
Socket.IO 客户端
Download
Maven:
com.koushikdutta.async
androidasync
(insert latest version)
Gradle:
dependencies {
compile 'com.koushikdutta.async:androidasync:2.+'
}
将url下载为一个字符串
// url is the URL to download.
AsyncHttpClient.getDefaultInstance().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);
}
});
从url下载JSON
// url is the URL to download.
AsyncHttpClient.getDefaultInstance().getJSONObject(url, new AsyncHttpClient.JSONObjectCallback() {
// Callback is invoked with any exceptions/errors, and the result, if available.
@Override
public void onCompleted(Exception e, AsyncHttpResponse response, JSONObject result) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("I got a JSONObject: " + result);
}
});
JSON Arrays
// url is the URL to download.
AsyncHttpClient.getDefaultInstance().getJSONArray(url, new AsyncHttpClient.JSONArrayCallback() {
// Callback is invoked with any exceptions/errors, and the result, if available.
@Override
public void onCompleted(Exception e, AsyncHttpResponse response, JSONArray result) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("I got a JSONArray: " + result);
}
});
将url下载为文件
AsyncHttpClient.getDefaultInstance().getFile(url, filename, new AsyncHttpClient.FileCallback() {
@Override
public void onCompleted(Exception e, AsyncHttpResponse response, File result) {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("my file is available at: " + result.getAbsolutePath());
}
});