Search in sources :

Example 1 with RequestFilter

use of com.android.volley.RequestQueue.RequestFilter in project fresco by facebook.

the class VolleyNetworkFetcher method fetch.

@Override
public void fetch(final VolleyNetworkFetchState fetchState, final Callback callback) {
    fetchState.submitTime = SystemClock.elapsedRealtime();
    final RawRequest request = new RawRequest(fetchState.getUri().toString(), new Response.Listener<byte[]>() {

        @Override
        public void onResponse(byte[] bytes) {
            fetchState.responseTime = SystemClock.uptimeMillis();
            try {
                InputStream is = new ByteArrayInputStream(bytes);
                callback.onResponse(is, bytes.length);
            } catch (IOException e) {
                callback.onFailure(e);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            callback.onFailure(volleyError);
        }
    });
    fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            mRequestQueue.cancelAll(new RequestFilter() {

                @Override
                public boolean apply(Request<?> candidate) {
                    return candidate != null && request.getSequence() == candidate.getSequence();
                }
            });
        }
    });
    mRequestQueue.add(request);
}
Also used : VolleyError(com.android.volley.VolleyError) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.android.volley.Request) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) Response(com.android.volley.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestFilter(com.android.volley.RequestQueue.RequestFilter)

Aggregations

Request (com.android.volley.Request)1 RequestFilter (com.android.volley.RequestQueue.RequestFilter)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 BaseProducerContextCallbacks (com.facebook.imagepipeline.producers.BaseProducerContextCallbacks)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1