参考:
Android框架学习笔记02AndroidAsycHttp框架
android AsyncHttp的简单使用与封装
官网:
android-async-http
android-async-http是一种异步请求网络的方式,请求回调类有:JsonHttpResponseHandler
、AsyncHttpResponseHandler
,RequestParams
采用put()或add()
添加请求参数
相应的核心类的介绍:
(1).AsyncHttpResponseHandler:请求返回处理成功、失败、开始、完成等自定义的消息的类。
(2).BinaryHttpResponseHandler:AsyncHttpResponseHandler的子类。该类是一个字节流返回处理的类。用于处理图片等。返回数据类型是byte[]
(3).TextHttpResponseHandler返回数据类型是String,其实就是把BinaryHttpResponseHandler返回的byte[]转成了String返回给我们。它是一个抽象类,所以我们在new的时候,onSuccess()不会自动生成,需要我们重写该方法。
(4).JsonHttpResponseHandler:AsyncHttpResponseHandler的子类。这是一个服务器与客户端之间用Json数据交流时使用的类。客户端请求服务器的参数是Json数据类型的,服务器返回给客户端的数据也是Json数据类型的。返回数据类型是JsonObject或JsonArray。
其实就是把TextHttpResponseHandler返回的String转成了JsonObject/ObjectArray返回给我们。它是一个抽象类,所以我们在new的时候,onSuccess()不会自动生成,需要我们重写该方法。
所以重新onSuccess不能使用返回参数是String的那个方法。
(4).RequestParams:封装参数处理的类。将客户端请求的参数封装在该类中。
(5).AsyncHttpClient:异步客户端请求的类。
(6).SyncHttpClient:同步客户端请求的类。AsyncHttpClient的子类。
dependencies {
...
compile 'com.loopj.android:android-async-http:1.4.9'
...
}
String url = "http://192.168.1.11:8080/okhttp/json3";
AsyncHttpClient http = new AsyncHttpClient();
http.get(url, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
String result = response.toString();
Logger.d("onSuccess=" + result);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Logger.d("onFailure=" + responseString);
}
});
String url = "http://apicloud.mob.com/v1/mobile/address/query";
AsyncHttpClient http = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("key", "1922d51a5257d");//put(...)可以刻传递参数
params.add("phone", "15153519007");//号码已作废
http.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String result = new String(responseBody);
Logger.d("onSuccess=" + result);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
String result = responseBody.toString();
Logger.d("onFailure=" + result);
}
});
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
String url = "http://192.168.1.11:8080/QQ/QQ.apk";
AsyncHttpClient http = new AsyncHttpClient();
http.get(url, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
dialog.setTitle("提示信息");
dialog.setMessage("下载中...");
dialog.show();
}
@Override
public void onProgress(final long bytesWritten, final long totalSize) {
super.onProgress(bytesWritten, totalSize);
dialog.setMax((int) totalSize);
dialog.setProgress((int) bytesWritten);
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
dialog.dismiss();
Logger.d("onFailure=下载成功");
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
String result = responseBody.toString();
dialog.dismiss();
Logger.d("onFailure=下载失败");
}
});
String url = "http://192.168.1.11:8080/okhttp/json3";
AsyncHttpClient http = new AsyncHttpClient();
RequestParams params = new RequestParams();
File file = new File("/mnt/sdcard0/qq.apk");
try {
params.put("file", file);
http.get(url, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Logger.d("上传成功");
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Logger.d("上传失败");
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
public static void cancelAllRequests() {
client.cancelAllRequests(true);
}
将get和post请求写成静态方法
import com.loopj.android.http.*;
public class TwitterRestClient {
private static final String BASE_URL = "https://api.twitter.com/1/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
自己修改的
public class AppRestClient {
private static final String BASE_URL = StringConstant.BASE_URL;
private static AsyncHttpClient client = new AsyncHttpClient();
/**
* 无参
*/
public static void getBytes(String url, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), responseHandler);
}
public static void postBytes(String url, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), responseHandler);
}
public static void getJson(String url, JsonHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), responseHandler);
}
public static void postJson(String url, JsonHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), responseHandler);
}
/**
* 有参
*/
// AsyncHttpResponseHandler
public static void getBytes(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void postBytes(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
//JsonHttpResponseHandler
public static void getJson(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void postJson(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
网络请求框架(一):android-async-http
网络请求框架(二):xUtils
网络请求框架(三):Volley
Volley的使用(一):get+post
Volley的使用(二):加载网络图片
Volley的使用(三):Volley与Activity的联动 + Volley的二次封装
网络请求框架(四):OkHttp
网络请求框架(五):Retrofit