Android中关于Volley的使用(十)对Request和Reponse的认识

我们知道,在网络Http通信中,一定会有一个Request,同样的,也一定会有一个Response,而我们在Volley中利用RequestQueue来添加请求之前,一定会先创建一个Request对象,比如StringRequest,JsonObjectRequest和ImageRequest等,如下分别是前面Demo中的JsonRequest和ImageRequest:

JsonObjectRequest:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,  
  2.         ErrorListener errorListener) {  

ImageRequest:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,  
  2.         Config decodeConfig, Response.ErrorListener errorListener)   

Volley中提供了一个基础的Request抽象类,如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public abstract class Request<T> implements Comparable<Request<T>> {  

在这个类中,定义了一些请求中基本的参数变量,如

Method:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Request method of this request.  Currently supports GET, POST, PUT, DELETE, HEAD, OPTIONS, 
  3.  * TRACE, and PATCH. 
  4.  */  
  5. private final int mMethod;  
它的值如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Supported request methods. 
  3.  */  
  4. public interface Method {  
  5.     int DEPRECATED_GET_OR_POST = -1;  
  6.     int GET = 0;  
  7.     int POST = 1;  
  8.     int PUT = 2;  
  9.     int DELETE = 3;  
  10.     int HEAD = 4;  
  11.     int OPTIONS = 5;  
  12.     int TRACE = 6;  
  13.     int PATCH = 7;  
  14. }  

请求中的Url:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** URL of this request. */  
  2. private final String mUrl;  

一个ErroListener,

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** Listener interface for errors. */  
  2. private final Response.ErrorListener mErrorListener;  

还有其它的一些参数,如shouldCache(是否需要缓存),tag(分类标签)等,而在Request中还提供了下面两个抽象方法,必须由子类实现:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Subclasses must implement this to parse the raw network response 
  3.  * and return an appropriate response type. This method will be 
  4.  * called from a worker thread.  The response will not be delivered 
  5.  * if you return null. 
  6.  * @param response Response from the network 
  7.  * @return The parsed response, or null in the case of an error 
  8.  */  
  9. abstract protected Response<T> parseNetworkResponse(NetworkResponse response);  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Subclasses must implement this to perform delivery of the parsed 
  3.  * response to their listeners.  The given response is guaranteed to 
  4.  * be non-null; responses that fail to parse are not delivered. 
  5.  * @param response The parsed response returned by 
  6.  * {@link #parseNetworkResponse(NetworkResponse)} 
  7.  */  
  8. abstract protected void deliverResponse(T response);  

每一个子类都必须实现两个方法,

1)parseNetworkResponse

当从网络中获取到Response的时候,怎么去解析对应的请求,这是由各个对应的Request去分析的,比如JsonObjectRequest中:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {  
  3.     try {  
  4.         String jsonString =  
  5.             new String(response.data, HttpHeaderParser.parseCharset(response.headers));  
  6.         return Response.success(new JSONObject(jsonString),  
  7.                 HttpHeaderParser.parseCacheHeaders(response));  
  8.     } catch (UnsupportedEncodingException e) {  
  9.         return Response.error(new ParseError(e));  
  10.     } catch (JSONException je) {  
  11.         return Response.error(new ParseError(je));  
  12.     }  
  13. }  

再比如ImageRequest中的:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {  
  3.     // Serialize all decode on a global lock to reduce concurrent heap usage.  
  4.     synchronized (sDecodeLock) {  
  5.         try {  
  6.             return doParse(response);  
  7.         } catch (OutOfMemoryError e) {  
  8.             VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl());  
  9.             return Response.error(new ParseError(e));  
  10.         }  
  11.     }  
  12. }  

而在doParse中,其实是对图片进行处理,如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private Response<Bitmap> doParse(NetworkResponse response) {  
  2.     byte[] data = response.data;  
  3.     BitmapFactory.Options decodeOptions = new BitmapFactory.Options();  
  4.     Bitmap bitmap = null;  
  5.     if (mMaxWidth == 0 && mMaxHeight == 0) {  
  6.         decodeOptions.inPreferredConfig = mDecodeConfig;  
  7.         bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);  
  8.     } else {  
  9.         // If we have to resize this image, first get the natural bounds.  
  10.         decodeOptions.inJustDecodeBounds = true;  
  11.         BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);  
  12.         int actualWidth = decodeOptions.outWidth;  
  13.         int actualHeight = decodeOptions.outHeight;  
  14.   
  15.         // Then compute the dimensions we would ideally like to decode to.  
  16.         int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight,  
  17.                 actualWidth, actualHeight);  
  18.         int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth,  
  19.                 actualHeight, actualWidth);  
  20.   
  21.         // Decode to the nearest power of two scaling factor.  
  22.         decodeOptions.inJustDecodeBounds = false;  
  23.         // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?  
  24.         // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;  
  25.         decodeOptions.inSampleSize =  
  26.             findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);  
  27.         Bitmap tempBitmap =  
  28.             BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);  
  29.   
  30.         // If necessary, scale down to the maximal acceptable size.  
  31.         if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||  
  32.                 tempBitmap.getHeight() > desiredHeight)) {  
  33.             bitmap = Bitmap.createScaledBitmap(tempBitmap,  
  34.                     desiredWidth, desiredHeight, true);  
  35.             tempBitmap.recycle();  
  36.         } else {  
  37.             bitmap = tempBitmap;  
  38.         }  
  39.     }  
  40.   
  41.     if (bitmap == null) {  
  42.         return Response.error(new ParseError(response));  
  43.     } else {  
  44.         return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));  
  45.     }  
  46. }  

所以,如果我们自定义一个Request的话,我们就要去实现我们自己的逻辑,比如是获取视频的话,就会去对数据进行解码等。

在上面的方法实现中,我们可以看到,最后都是通过Response.success方法返回一个Response对象,而这个Response对象是怎么用的呢,就要看下面deliverResponse方法了。

2)deliverResponse

在NetworkDispatcher线程中,当从网络中获取到数据,并通过请求的parseNetworkResponse方法解析之后,会返回一个Reponse对象,这个时候,就会调用Executor来将这个请求post回主线程,如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mDelivery.postResponse(request, response);  

而mDelivery中的postResponse方法其实是另起一个新线程来调用Request的deliverResponse方法,在ExecutorDelivery类中:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {  
  2.         request.markDelivered();  
  3.         request.addMarker("post-response");  
  4.         mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));  
  5.     }  

ResponseDeliveryRunnable类的run方法中,我们可以看到:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Deliver a normal response or error, depending.  
  2. if (mResponse.isSuccess()) {  
  3.     mRequest.deliverResponse(mResponse.result);  
  4. else {  
  5.     mRequest.deliverError(mResponse.error);  
  6. }  

那我们看看StringRequest和ImageRequest中的deliverResponse方法:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private final Response.Listener<Bitmap> mListener;  
  2. ...  
  3. @Override  
  4.     protected void deliverResponse(Bitmap response) {  
  5.         mListener.onResponse(response);  
  6.     }  
我们可以看到,其实都是调用一个Response.Listener类的onResponse方法,而其实这个Listener,则是我们在创建请求的时候才实现,并传进来的,如前面Demo中创建JsonObjectRequest和ImageRequest的时候:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ImageRequest imgRequest = new ImageRequest(imgUrl,  
  2.                 new Response.Listener<Bitmap>() {  
  3.                     @Override  
  4.                     public void onResponse(Bitmap arg0) {  
  5.                         // TODO Auto-generated method stub  
  6.                         imageView.setImageBitmap(arg0);  
  7.                     }  
  8.                 },   
  9.                 100,   
  10.                 100,   
  11.                 Config.ARGB_8888,   
  12.                 new ErrorListener() {  
  13.                     @Override  
  14.                     public void onErrorResponse(VolleyError arg0) {  
  15.                         imageView.setImageResource(R.drawable.ic_launcher);  
  16.                     }  
  17.                 });  

如上面new Reponse.Listener方法中的实现,很显然,之所以这么做的原因在于只有调用者才知道怎么去处理Request解析过的数据。

而从这里,我们也可以知道在Reponse类中,就会定义这么一个接口,如下,是Volley中Response类的定义:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Response<T> {  
  2.   
  3.     /** Callback interface for delivering parsed responses. */  
  4.     public interface Listener<T> {  
  5.         /** Called when a response is received. */  
  6.         public void onResponse(T response);  
  7.     }  

而除了这个接口的定义,还有一个ErrorListener接口的定义就不列出来了。而除此之外,Response类中就存放了CacheEntry等信息,相对来说,因为定义了这样的Listener接口,Response类是相对比较简单的。

好了,到这里,总结一下:

1)创建一个Request的时候,会同时设置一个Response.Listener作为请求的一个参数变量,之后调用RequestQueue的add方法将其添加到Queue。

2)在Queue中的请求会由NetworkDispatcher去跟网络进行通信(如果有缓存的时候,就是CacheDispatcher)。

3)当请求结果回来的时候,Request会首先调用parseNetworkResponse方法根据不同的请求类型,如Json,Image等进行不同的处理。

4)当Request分析完之后,得到的Reponse对象,就会由ResponseDelivery类新起一个线程,调用1)步中的Listener来进行处理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值