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来进行处理。

结束。


posted on 2017-06-07 11:02  blfbuaa  阅读(237)  评论(0编辑  收藏  举报