您的位置:首页 > 理论基础 > 计算机网络

Volley高并发处理网络请求(No1)

2016-04-27 22:38 441 查看
这是我的第一篇博客,那些地方不规范多多包涵,声明转载他人的。最近学习了Volley框架,感觉在处理网络轻量级数据时特别好用,所以将自己学习过程觉得好的2篇文章拿来分享!!

No 001:转载于点击打开链接

Volley简介:

Volley是google在13年I/O大会上发布的有关网络请求的框架,其目的是为了解决HttpUrlConnect和HttpClient(在android6.0中已经被废弃)使用不方便的问题,从字面上我们就可以看出来此框架的优点----Volley(齐射,发射),就好像很多人一起射箭,注意:是射箭,不是很多人一起发大炮,这点很重要,也是volley的一个特性,它支持数据量不大,但是请求频繁的网络请求,如请求文字,图片,然而,如果是音频和视频的下载,这就属于发大炮的级别了,volley可玩不转,所以使用的时候一定要注意。

为什么Android中网络请求如此重要?

顺便说一下,其实我们目前的app也仅仅是一个客户端,一个服务器的窗口,用户要的所有东西都存在服务器中,而app所能作的只是使用户的眼睛能够更方便的看到服务器中的内容,从某种意义上来说,app和网站的前端口还是比较像的,因此,一旦我们理解了这一点,我们就能给我们的app,甚至是我们的工作做一个定位----对于大多数android开发工程师来说,所做的工作就是通过网络把服务器上的东西呈现给用户,所以从这一点可以看出,对于用户来说,内容>=界面>=性能,这只是我对android的理解,以及我理解的网络请求对android的重要性。。

Google为什么推出Volley?

因为在google推出Volley之前有很多的请求网络的方式,所以在googel推出Volley,我的第一个反应是google为什么推出Volley,它有什么优势?

优势一:可以让我们避开HttpUrlConnection和HttpClient,用这两种方式请求网络,在早些时候很普遍,但是会经常出现一些问题,并且这两个类中的一些bugs很长时间没有被修复,导致问题长期的存在,但是值得注意的是,Volley其中封装的也是HttpUrlConnection和HttpClient。

优势二:速度快,google曾经对volley做过很多的测试,发现volley相比其他的请求方式的得分高达数倍。

优势三:可以缓存所有的东西,使用户的体验更好,加载速度更快。




Volley怎么使用?

Volley的使用非常简单,因为是数据两不大的请求,所以主要是对两种资源的请求:

请求文本:

一,建立一个请求队列,队列中存放的是对网络的请求,因为想让整个程序公用一个请求队列,所以在此把请求队列定义在Application中;

public class appication extends Application{
//把请求队列定义称为全局的
public static RequestQueue queue;
@Override
public void onCreate() {
super.onCreate();
queue = Volley.newRequestQueue(getApplicationContext());
}
public static  RequestQueue getRequestQueue(){
return queue;
}
}


二,创建网络请求对象;

对于文本类的请求对象,Volley常用的有三个:StringRequest,JsonObjectRequest和JsonArrayRequest;

文本类StringRequest的Get请求:

private void volley_Get() {
String url = "http://api.juheapi.com/japi/toh?key=7bc8ff86168092de65576a6166bfc47b&v=1.0&month=11&day=1";
StringRequest strrequest = new StringRequest(Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this,volleyError.toString(),Toast.LENGTH_LONG).show();
}
});
strrequest.setTag("strrequest");//设置一个标签
appication.getRequestQueue().add(strrequest);//将请求加入到队列

}


文本类JsonObjectRequest的Get请求:

private void jsonObject_Get() {
String url = "http://api.juheapi.com/japi/toh?key=7bc8ff86168092de65576a6166bfc47b&v=1.0&month=11&day=1";
JsonObjectRequest jsonobject = new JsonObjectRequest(Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast.makeText(MainActivity.this,jsonObject.toString(),Toast.LENGTH_LONG).show();

}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this,volleyError.toString(),Toast.LENGTH_LONG).show();

}
});
jsonobject.setTag("strrequest");//设置一个标签
appication.getRequestQueue().add(jsonobject);//将请求加入到队列
}

文本类StringRequest的Post请求:

private void volley_Post() {
String url = "http://api.juheapi.com/japi/toh?";
StringRequest request = new StringRequest(Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this,s.toString(),Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this,volleyError.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
//使用post请求方式,要把参数写在此方法中
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String,String>();
map.put("key","7bc8ff86168092de65576a6166bfc47b");
return map;
}
};
request.setTag("strrequest");//设置一个标签
appication.getRequestQueue().add(request);//将请求加入到队列
}

文本类JsonObjectRequest的Post请求:

private void jsonvolley_Post() {
String url = "http://api.juheapi.com/japi/toh?";
Map<String,String> map = new HashMap<String,String>();
map.put("key","7bc8ff86168092de65576a6166bfc47b");
JSONObject jsonobj = new JSONObject(map);
JsonObjectRequest jsonrequest = new JsonObjectRequest(Method.POST, url, jsonobj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
});
jsonrequest.setTag("strrequest");//设置一个标签
appication.getRequestQueue().add(jsonrequest);//将请求加入到队列
}


请求图片:Volley请求图片有三种方式:

一,不带缓存的请求:

private void loadImage() {
String url = "https://www.baidu.com/img/bdlogo.png";
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageview.setImageBitmap(bitmap);
}
}, 0, 0, FIT_CENTER, Config.ARGB_8888, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
}
});
imageRequest.setTag("imagerequest");
appication.getRequestQueue().add(imageRequest);
}

二,带缓存,使用ImageView进行显示的请求:

首相建一个缓存类:

public class BitmapCache implements ImageLoader.ImageCache {
LruCache<String,Bitmap> lruCache = null;
public int max = 1024*1024*10;//缓存的大小,如果超出此大小,就启动回收
public BitmapCache(){
lruCache = new LruCache<String,Bitmap>(max){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes()*value.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String s) {
return null;
}

@Override
public void putBitmap(String s, Bitmap bitmap) {

}
}

加载类:

private void secondLoadImage() {
String url = "https://www.baidu.com/img/bdlogo.png";
ImageLoader imageLoader = new ImageLoader(appication.getRequestQueue(),new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(imageview,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
imageLoader.get(url,listener);
}

三,使用NetworkImageView显示图片:

NetworkImageView是一个自定义的控件,直接在布局文件中使用即可。

<com.android.volley.toolbox.NetworkImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/networkimage"
/>


加载类:

private void networkimageview() {
String url = "https://www.baidu.com/img/bdlogo.png";
ImageLoader imageLoader = new ImageLoader(appication.getRequestQueue(),new BitmapCache());
networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
networkImageView.setImageUrl(url,imageLoader);
}


最后:Volley的网络请求可以实现和Activity的联动:

//与activity联动
@Override
protected void onStop() {
super.onStop();
//把队列中的对应的网络请求取消
appication.getRequestQueue().cancelAll("strrequest");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: