Android-async-http框架的二次封装
Android-async-http的简介及特点
Async-http是一款国外开源框架。高效的网络数据请求,文件下载和上传。
特点:
1.清晰的网络请求回调
2.请求使用ThreadPool,限制并发资源使用情况
3.GEt/POST基于参数构建的使用(RequestParams),方便
4.Multipart文件上传,大数据上传下载
5.自动智能请求重试,优化了质量不一的移动连接
6.内置相应解析成JSON,使用JsonHttpResponseHandler
7.持久化cookie存储,保存cookie到你的应用程序的SharedPreferences
8.二进制文件(图片等)的下载,使用BinaryHttpResponseHandler
创建一个MyAsyncHttpDemo项目,并将android-async-http-1.4.3.jar包导入libs文件夹中
接下来在xml文件中给TextView添加一个id:android:id="@+id/tv"
在MainActivity.java源文件中,分别常创建两个方法,并在onCreate方法中调用即可:
private void asyncPost(){
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://www.tuling123.com/openapi/api?";
RequestParams params = new RequestParams();
params.put("key", "4031b38733e55228d0eb123eba1471f9");
params.put("info", "北京今天天气");
client.post(url, params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String arg0) {
Log.e("onSuccess", "成功返回数据:"+arg0);
tv.setText(arg0);
Toast.makeText(MainActivity.this, arg0, 1).show();
super.onSuccess(arg0);
}
@Override
public void onFailure(Throwable arg0) {
Log.d("onFailure", "请求数据失败");
Toast.makeText(MainActivity.this, "网络请求失败", 1).show();
super.onFailure(arg0);
}
});
}
private void asyncGet() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://www.tuling123.com/openapi/api?key=4031b38733e55228d0eb123eba1471f9&info=北京今天天气";
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0) {
super.onFailure(arg0);
Log.d("onFailure", "请求数据失败");
Toast.makeText(MainActivity.this, "网络请求失败", 1).show();
}
@Override
public void onSuccess(String arg0) {
super.onSuccess(arg0);
Log.e("onSuccess", "成功返回数据:"+arg0);
tv.setText(arg0);
Toast.makeText(MainActivity.this, arg0, 1).show();
}
});
}
为了是更方便的应用,我们可以对其进行二次封装,新建一个继承自AsyncHttpResponseHandler的类:
public abstract class NetCallBack extends AsyncHttpResponseHandler {
@Override
public void onFailure(Throwable arg0) {
Log.d("info", "请求失败,隐藏进度条"+ arg0);
onMyFailure(arg0);
super.onFailure(arg0);
}
@Override
public void onStart() {
Log.d("info", "请求数据开始,弹出进度条框");
super.onStart();
}
@Override
public void onSuccess(String arg0) {
Log.d("info", "请求成功,隐藏进度条"+ arg0);
onMySuccess(arg0);
super.onSuccess(arg0);
}
public abstract void onMySuccess(String result);
public abstract void onMyFailure(Throwable arg0);
}
再新建一个工具类:
public class RequestUtils {
public static AsyncHttpClient client = new AsyncHttpClient();
public static void clientGet(String url, NetCallBack cb) {
client.get(url, cb);
}
public static void clientPost(String url, RequestParams params,
NetCallBack cb) {
client.post(url, params, cb);
}
}
再在MainActivity.java源文件中:
private void asyncHttpPost() {
String url = "http://www.tuling123.com/openapi/api?";
RequestParams params = new RequestParams();
params.put("key", "4031b38733e55228d0eb123eba1471f9");
params.put("info", "北京今天天气");
RequestUtils.clientPost(url, params, new NetCallBack() {
@Override
public void onMySuccess(String result) {
Log.e("onMySuccess", "成功返回数据:"+ result);
tv.setText(result);
Toast.makeText(MainActivity.this, result, 1).show();
}
@Override
public void onMyFailure(Throwable arg0) {
Log.d("onMyFailure", "请求数据失败");
Toast.makeText(MainActivity.this, "网络请求失败", 1).show();
}
});
}
private void asyncHttpGet(){
String url = "http://192.168.1.114/MyApp/getMeinvJson.php";
RequestUtils.clientGet(url, new NetCallBack() {
@Override
public void onMySuccess(String result) {
Log.e("onMySuccess", "成功返回数据:"+ result);
tv.setText(result);
Toast.makeText(MainActivity.this, result, 1).show();
}
@Override
public void onMyFailure(Throwable arg0) {
Log.d("onMyFailure", "请求数据失败");
Toast.makeText(MainActivity.this, "网络请求失败", 1).show();
}
});
}
访问网络数据记得在清单文件中添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步