1.Volley
特点:
能使网络通信更快,更简单,更健壮
Get,Post网络请求及网络图像的高效率异步处理请求
可以对网络请求进行排序优先级管理
支持网络请求的缓存
多级别取消请求
使用Volley可以简化一些网络通信的开发。不适合大数据和流媒体的网络请求。例如上百兆文件、视频下载。
基本用法:
Get和Post用于与服务器间请求和发送数据
建立请求首先建立队列(RequestQueue),将请求添加到请求队列里
然后进行相应的Get和Post请求,请求结果通过两个监听器,在回调里获取解析
/**
*
* 1、Volley的Get和Post请求方法
*
* 2、Volley的网络请求队列建立和取消队列请求及Activity周期关联
*
*/
// 首先重写一个自定义的MyApplicaiton
public class MyApplication extends Application {
public static RequestQueue queue;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
queue = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueue() {
return queue;
}
}
// MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volley_Get();
volley_Post();
}
private void volley_Post() {
String url = ""; // 加入url地址
StringRequest request = new StringRequest(Method.POST, url,
new Listener() {
@Override
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0,
Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, "错误",
Toast.LENGTH_LONG).show();
}
}) {
protected Map getParams() throws AuthFailureError {
HashMap map = new HashMap();
map.put("", ""); // 放相关的数据比如AppKey
return map;
};
};
request.setTag("xuPost"); // 设置标签
MyApplication.getHttpQueue().add(request); // 添加到请求队列
}
private void volley_Get() {
String url = ""; // 加入url地址
StringRequest request = new StringRequest(Method.GET, url,
new Listener() {
@Override
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0,
Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, "错误",
Toast.LENGTH_LONG).show();
}
});
request.setTag("xuGet"); // 设置标签
MyApplication.getHttpQueue().add(request); // 添加到请求队列
}
// 取消请求
@Override
protected void onStop() {
super.onStop();
MyApplication.getHttpQueue().cancelAll("xuPost");
MyApplication.getHttpQueue().cancelAll("xuGet");
}
}
需要在配置文件中添加网络权限!
2.Async-http
特点:
清晰的网络请求回调
请求使用ThreadPool,限制并发资源使用情况
GET/POST基于参数构建使用(RequestParams)
支持Multipart文件上传,大数据上传下载
内置响应解析成JSON
持久化cookie存储,保存cookie到你的应用程序的SharedPreferences
支持二进制文件(图片等)的下载
/**
*
* 1.Android-async-http的Get和Post请求方法的使用
*
* 2.Android-async-http回调逻辑的二次封装
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asynchttpGet();
asynchttpPost();
}
private void asynchttpGet() {
// 二次封装具体使用方法
// String url = "";
// RequestUtils.ClientGet(url, new mNetCallBack() {
//
// @Override
// public void onMySuccess(String result) {
// // TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
// }
//
// @Override
// public void onMyFailure(Throwable arg0) {
// // TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_LONG).show();
// }
// });
AsyncHttpClient client = new AsyncHttpClient();
String url = ""; // 加入url地址
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String arg0) {
// TODO Auto-generated method stub
super.onSuccess(arg0);
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
.show();
}
@Override
public void onFailure(Throwable arg0) {
// TODO Auto-generated method stub
super.onFailure(arg0);
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
.show();
}
});
}
private void asynchttpPost() {
// TODO Auto-generated method stub
AsyncHttpClient client = new AsyncHttpClient();
String url = ""; // 加入url地址
// 通过RequestParams添加具体提交参数
RequestParams params = new RequestParams();
params.put("phone", "");
params.put("apikey", "");
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String result) {
// TODO Auto-generated method stub
super.onSuccess(result);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG)
.show();
}
@Override
public void onFailure(Throwable throwable) {
// TODO Auto-generated method stub
super.onFailure(throwable);
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
.show();
}
});
}
}
// 二次封装具体操作类,RequestUtils.java
public class RequestUtils {
public static AsyncHttpClient client = new AsyncHttpClient();
public static void ClientGet(String url, mNetCallBack cb) {
client.get(url, cb);
}
public static void ClientPost(String url, RequestParams params,
mNetCallBack cb) {
client.post(url, params, cb);
}
}
public abstract class mNetCallBack extends AsyncHttpResponseHandler {
@Override
public void onStart() {
Log.i("mNetCallback", "请求开始");
super.onStart();
}
@Override
public void onSuccess(String result) {
Log.i("mNetCallback", "请求成功" + result);
onMySuccess(result);
super.onSuccess(result);
}
@Override
public void onFailure(Throwable throwable) {
Log.i("mNetCallback", "请求失败");
onMyFailure(throwable);
super.onFailure(throwable);
}
public abstract void onMySuccess(String result);
public abstract void onMyFailure(Throwable throwable);
}
记住也需要添加网络权限!
简单来说,两个框架都非常实用,不过一个支持大文件下载上传,没有绝对的好与坏,看你的项目具体用途吧~
不过最近流行的框架okhttp以及Retrofit(两个都是大名鼎鼎的Square出品)也是挺好用的,大家自己可以尝试一下~