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

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

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

JsonObjectRequest:

    public JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,
            ErrorListener errorListener) {

ImageRequest:

    public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
            Config decodeConfig, Response.ErrorListener errorListener) 

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

public abstract class Request<T> implements Comparable<Request<T>> {

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

Method:

    /**
     * Request method of this request.  Currently supports GET, POST, PUT, DELETE, HEAD, OPTIONS,
     * TRACE, and PATCH.
     */
    private final int mMethod;
它的值如下:

    /**
     * Supported request methods.
     */
    public interface Method {
        int DEPRECATED_GET_OR_POST = -1;
        int GET = 0;
        int POST = 1;
        int PUT = 2;
        int DELETE = 3;
        int HEAD = 4;
        int OPTIONS = 5;
        int TRACE = 6;
        int PATCH = 7;
    }

请求中的Url:

    /** URL of this request. */
    private final String mUrl;

一个ErroListener,

    /** Listener interface for errors. */
    private final Response.ErrorListener mErrorListener;

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

    /**
     * Subclasses must implement this to parse the raw network response
     * and return an appropriate response type. This method will be
     * called from a worker thread.  The response will not be delivered
     * if you return null.
     * @param response Response from the network
     * @return The parsed response, or null in the case of an error
     */
    abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

    /**
     * Subclasses must implement this to perform delivery of the parsed
     * response to their listeners.  The given response is guaranteed to
     * be non-null; responses that fail to parse are not delivered.
     * @param response The parsed response returned by
     * {@link #parseNetworkResponse(NetworkResponse)}
     */
    abstract protected void deliverResponse(T response);

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

1)parseNetworkResponse

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

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

再比如ImageRequest中的:

    @Override
    protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
        // Serialize all decode on a global lock to reduce concurrent heap usage.
        synchronized (sDecodeLock) {
            try {
                return doParse(response);
            } catch (OutOfMemoryError e) {
                VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl());
                return Response.error(new ParseError(e));
            }
        }
    }

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

    private Response<Bitmap> doParse(NetworkResponse response) {
        byte[] data = response.data;
        BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        Bitmap bitmap = null;
        if (mMaxWidth == 0 && mMaxHeight == 0) {
            decodeOptions.inPreferredConfig = mDecodeConfig;
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        } else {
            // If we have to resize this image, first get the natural bounds.
            decodeOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
            int actualWidth = decodeOptions.outWidth;
            int actualHeight = decodeOptions.outHeight;

            // Then compute the dimensions we would ideally like to decode to.
            int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight,
                    actualWidth, actualHeight);
            int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth,
                    actualHeight, actualWidth);

            // Decode to the nearest power of two scaling factor.
            decodeOptions.inJustDecodeBounds = false;
            // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
            // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
            decodeOptions.inSampleSize =
                findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
            Bitmap tempBitmap =
                BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

            // If necessary, scale down to the maximal acceptable size.
            if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||
                    tempBitmap.getHeight() > desiredHeight)) {
                bitmap = Bitmap.createScaledBitmap(tempBitmap,
                        desiredWidth, desiredHeight, true);
                tempBitmap.recycle();
            } else {
                bitmap = tempBitmap;
            }
        }

        if (bitmap == null) {
            return Response.error(new ParseError(response));
        } else {
            return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
        }
    }

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

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

2)deliverResponse

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

mDelivery.postResponse(request, response);

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

public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
        request.markDelivered();
        request.addMarker("post-response");
        mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
    }

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

            // Deliver a normal response or error, depending.
            if (mResponse.isSuccess()) {
                mRequest.deliverResponse(mResponse.result);
            } else {
                mRequest.deliverError(mResponse.error);
            }

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

private final Response.Listener<Bitmap> mListener;
...
@Override
    protected void deliverResponse(Bitmap response) {
        mListener.onResponse(response);
    }
我们可以看到,其实都是调用一个Response.Listener类的onResponse方法,而其实这个Listener,则是我们在创建请求的时候才实现,并传进来的,如前面Demo中创建JsonObjectRequest和ImageRequest的时候:

ImageRequest imgRequest = new ImageRequest(imgUrl,
				new Response.Listener<Bitmap>() {
					@Override
					public void onResponse(Bitmap arg0) {
						// TODO Auto-generated method stub
						imageView.setImageBitmap(arg0);
					}
				}, 
				100, 
				100, 
				Config.ARGB_8888, 
				new ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						imageView.setImageResource(R.drawable.ic_launcher);
					}
				});

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

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

public class Response<T> {

    /** Callback interface for delivering parsed responses. */
    public interface Listener<T> {
        /** Called when a response is received. */
        public void onResponse(T response);
    }

而除了这个接口的定义,还有一个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来进行处理。

结束。