一、什么是AsyncHttpClient?
Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnect,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息。
二、AsyncHttpClient的特性:
(1)采用异步http请求,并通过匿名内部类处理回调结果
(2)http请求独立在UI主线程之外
(3)采用线程池来处理并发请求
(4)采用RequestParams类创建GET/POST参数
(5)不需要第三方包即可支持Multipart file文件上传
(6)大小只有25kb
(7)自动为各种移动电话处理连接断开时请求重连
(8)超快的自动gzip响应解码支持
(9)使用BinaryHttpResponseHandler类下载二进制文件(如图片)
(10) 使用JsonHttpResponseHandler类可以自动将响应结果解析为json格式
(11)持久化cookie存储,可以将cookie保存到你的应用程序的SharedPreferences中
三、AsyncHttpClient的简单使用:
首先,在dependencies 下添加依赖:
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
1.不带参数使用get请求:
写一个HttpUtil类把这个方法封装起来:
AsyncHttpClient asyncHttpClient = new SyncHttpClient();
asyncHttpClient.get(BASE_URL, AsyncHttpClientRespenseHandler);
这样就完成了Get请求,BASE_URL是地址,AsyncHttpClientRespenseHandler是请求回调。
new AsyncHttpClientRespenseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//请求成功
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//请求失败
}
}
2.带参数的get请求:
请求一个接口,在HttpUtil类中代码如下:
在一个程序中可能会遇到不同的接口,在这里,我们只需要将接口地址相同的部分封装起来,在请求的时候,将需要使用的对应的地址拼接即可;
public class HttpUtil {
private static final String BASE_URL="http://103.244.59.105:8014/paopaoserver/";//接口相同地址
private static AsyncHttpClient client=new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.get(getAbsoluteUrl(url),params,responseHandler);
}
private static String getAbsoluteUrl(String relation){
return BASE_URL+relation;
}
}
在调用此方法请求数据出的代码如下:
RequestParams params=new RequestParams();
params.put("params","{\"page\":1,\"page_count\":10}");//需要请求数据的地址
HttpUtil.get("articles", params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(Main3Activity.this, "fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(Main3Activity.this, responseString, Toast.LENGTH_SHORT).show();
}
});
RequestParams是我们写进去的参数。
RequestParams的用法:
RequestParams requestParams=new RequestParams();
requestParams.put("key", what);
这里是以键值对的方式存进去的。
3.不带参数的Post请求:
public class HttpUtil {
private static final String BASE_URL="https://www.baidu.com/";
private static AsyncHttpClient client=new AsyncHttpClient();
private static void post(MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
client.setTimeout(20000);
client.post(BASE_URL, myHttpClientRespenseHandler);
}
}
4.带参数的Post请求:
使用方法与带参数的get请求方式相同
public class HttpUtil {
private static final String BASE_URL="http://103.244.59.105:8014/paopaoserver/";
private static AsyncHttpClient client=new AsyncHttpClient();
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relation){
return BASE_URL+relation;
}
}
使用post方法请求数据的代码如下:
RequestParams params=new RequestParams();
params.put("params","{\"page\":1,\"page_count\":10}");
HttpUtil.post("articles", params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(Main3Activity.this, responseString, Toast.LENGTH_SHORT).show();
}
});
使用Post请求还是get请求要根据API接口文档的要求而定
5.使用JSON来请求数据方法:
在一个程序中可能会用到JSON来请求数据,使用方法代码如下:
首先,还是先封装:
public class HttpUtil {
public static final String BASE_URL ="http://192.168.1.100:8890/type/jason/action/" ;
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(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler){
client.post(context,url,entity, contentType,responseHandler);
}
private static String getAbsoluteUrl(String relation){
return BASE_URL+relation;
}
}
请求数据的时候调用此方法:
首先定义一个JSONObject 对象放入需要解析的JSON键、值对
一个ByteArrayEntity对象,定义类型
JSONObject object=new JSONObject();
try {
object.put("Blower",0);
} catch (JSONException e) {
e.printStackTrace();
}
ByteArrayEntity entity=null;
try {
entity=new ByteArrayEntity(object.toString().getBytes("UTF-8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpUtil.post(mcontext, url+"control", entity, "application/json", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
number=0;
}
});
本文详细介绍AsyncHttpClient库在Android开发中的应用,包括其特点、优势及如何进行GET/POST请求等实用技巧。
544

被折叠的 条评论
为什么被折叠?



