AsyncHttpClient是Android中处理异步Http的方法。
使用AsyncHttpClient首先需要插入第三方的依赖库
使用Gradle包下面的build.gradel中添加依赖库
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
AsyncHttpClient有两种使用方法,一是写一个内部方法使用,二是封装AsyncHttpClient方法,通过调用来实现此方法
//创建一个新AsyncHttpClient实例并发出请求
AsyncHttpClient client = new AsyncHttpClient();
//AsyncHttpClient中有get和post两种方法
//第一个参数是所要获取接口,第二个参数是所发出的请求
client.get("https://www.google.com", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//用Toast显示是否请求成功
Toast.makeText(ShoudongActivity.this,"访问失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(ShoudongActivity.this,responseString,Toast.LENGTH_SHORT).show();
//打印获取到的信息
Log.e( "onSuccess: ", responseString+"+++++++++++++++++++++++++++++++++++");
}
});
由于在一个项目中使用的接口过多,如果在每一个类中都写一个AsyncHttpClient方法,那么就会使整个项目的代码量过大,而且比较繁琐,所以封装一个AsyncHttpClient方法,在需要的时候调用会使代码量减少
建立一个静态的Http客户端,先建立一个类来封装AsyncHttpClient方法
public class HttpUtil {
//先定义一个String类型来接收接口相同的部分
private static final String BASE_URL = "http://192.168.1.101:8890/type/jason/action/";
//建立静态的AsyncHttpClient
private static AsyncHttpClient client = new AsyncHttpClient();
//AsyncHttpClient中有get和post方法,需要用到public方法来修饰,以便调用
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.get(getAbsoluteUrl(url), params, responseHandler);
}
//post方法中HttpEntity参数是后面发送JSON格式所用到的一个方法
public static void post(Context context,String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
client.post(context,getAbsoluteUrl(url),entity, contentType, responseHandler);
}
//单独写一个方法添加URL
private static String getAbsoluteUrl(String url) {
return BASE_URL + url;
}
}
写完了封装AsyncHttpClient的方法类,那么接下来就要调用了
//在RequestParams类用于可选的get或post参数添加
RequestParams params=new RequestParams();
//用键值对的输出方式传出,第一个参数是键,第二个参数是值
params.put("params","{\"classify_id\":70,\"page\":1,\"page_count\":2}");
//第二个参数是上面RequestParams传来的参数
HttpUtil.get("getConfig", params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(ShoudongActivity.this,"访问失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(ShoudongActivity.this,responseString,Toast.LENGTH_SHORT).show();
Log.e( "onSuccess: ", responseString+"+++++++++++++++++++++++++++++++++++");
}
});
AsyncHttpClient的post方法发送JSON格式参数
用封装的AsyncHttpClient
//定义json对象
JSONObject jsonObject=new JSONObject();
try {
//解析json数据
jsonObject.put("Blower", 1);
} catch (JSONException e) {
e.printStackTrace();
}
//封装方法中post的参数
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
//用application/json向其传达这是json类型的接口数据
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//封装类.post发送数据
HttpUtil.post(CO2Activity.this, "control", entity, "application/json", new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
}
});