Volley.jar的使用

1.1 Volley简介

2013年Google I/O大会上推出了一个新的网络通信框架——Volley。Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。

1.2 Volley提供的功能
简单来说,它提供了如下的便利功能:

JSON,图像等的异步下载;

网络请求的排序(scheduling)

网络请求的优先级处理

缓存

多级别取消请求和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)

1.3 本次项目中Volley的使用

*自定义请求队列RequestQueue

package com.genting.membership.singleton;

import android.content.Context;

import com.android.volley.RequestQueue;

import com.android.volley.toolbox.ImageLoader;

import com.android.volley.toolbox.Volley;

import com.googlecode.androidannotations.annotations.EBean;

import com.googlecode.androidannotations.api.Scope;

import com.macrokiosk.utilities.BitmapLruCache;

@EBean(scope = Scope.Singleton)

public class VolleySingleton {

      

       public RequestQueue requestQueue;

       public ImageLoader imageLoader;  

 

       public VolleySingleton(Context context){

              if(context != null){

                    requestQueue = Volley.newRequestQueue(context);

                     imageLoader = new ImageLoader(requestQueue, new BitmapLruCache());

              }

       }

}

  *自定义任务类:JsonArrayRequest,StringRequest

public class JsonArrayRequest extends JsonRequest<JSONArray> {

 

       public JsonArrayRequest(int method, String url, JSONObject jsonRequest,

                     Listener<JSONArray> listener, ErrorListener errorListener) {

               super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),

                             listener, errorListener);

       }

 

       @Override

       protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {

              try {

                     String jsonString =

                                  new String(response.data, HttpHeaderParser.parseCharset(response.headers));

                     return Response.success(new JSONArray(jsonString),

                                  HttpHeaderParser.parseCacheHeaders(response));

              } catch (UnsupportedEncodingException e) {

                     return Response.error(new ParseError(e));

              } catch (JSONException je) {

                     return Response.error(new ParseError(je));

              }

       }

         @Override

         public RetryPolicy getRetryPolicy() {

              RetryPolicy mRetryPolicy=new DefaultRetryPolicy(60000, 1,1.0f);

              return mRetryPolicy;

         }

 

}

*加载任务,发送请求

       JsonArrayRequest request=new JsonArrayRequest(Method.GET,Constants.BASIC_URL,null, getUrlSuccess(), getError("getBasicUrl"));

       request.setRetryPolicy(new DefaultRetryPolicy(60 * 1000, 1, 1.0f));

    mRequestQueue.add(request);

*取消请求任务

 SoapRequest.setTag(this);其中this为设置的标签

 mVolley.requestQueue.cancelAll(this);其中this为刚才设置的标签

转载于:https://www.cnblogs.com/ruirui2015/p/4923630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值