Search in sources :

Example 16 with VolleyError

use of com.android.volley.VolleyError 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)

Example 17 with VolleyError

use of com.android.volley.VolleyError in project iosched by google.

the class BasicNetwork method attemptRetryOnException.

/**
     * Attempts to prepare the request for a retry. If there are no more attempts remaining in the
     * request's retry policy, a timeout exception is thrown.
     * @param request The request to use.
     */
private static void attemptRetryOnException(String logPrefix, Request<?> request, VolleyError exception) throws VolleyError {
    RetryPolicy retryPolicy = request.getRetryPolicy();
    int oldTimeout = request.getTimeoutMs();
    try {
        retryPolicy.retry(exception);
    } catch (VolleyError e) {
        request.addMarker(String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout));
        throw e;
    }
    request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout));
}
Also used : VolleyError(com.android.volley.VolleyError) RetryPolicy(com.android.volley.RetryPolicy)

Example 18 with VolleyError

use of com.android.volley.VolleyError in project iosched by google.

the class ImageLoader method get.

/**
     * Issues a bitmap request with the given URL if that image is not available
     * in the cache, and returns a bitmap container that contains all of the data
     * relating to the request (as well as the default image if the requested
     * image is not available).
     * @param requestUrl The url of the remote image
     * @param imageListener The listener to call when the remote image is loaded
     * @param maxWidth The maximum width of the returned image.
     * @param maxHeight The maximum height of the returned image.
     * @return A container object that contains all of the properties of the request, as well as
     *     the currently available image (default if remote is not loaded).
     */
public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();
    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }
    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, imageListener);
    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);
    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }
    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest = new ImageRequest(requestUrl, new Listener<Bitmap>() {

        @Override
        public void onResponse(Bitmap response) {
            onGetImageSuccess(cacheKey, response);
        }
    }, maxWidth, maxHeight, Config.RGB_565, new ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            onGetImageError(cacheKey, error);
        }
    });
    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}
Also used : ErrorListener(com.android.volley.Response.ErrorListener) VolleyError(com.android.volley.VolleyError) Bitmap(android.graphics.Bitmap) ImageRequest(com.android.volley.toolbox.ImageRequest)

Example 19 with VolleyError

use of com.android.volley.VolleyError in project OkVolley by googolmo.

the class RequestFragment method post.

private void post() {
    BaseRequest request = new BaseRequest(Request.Method.POST, "http://192.168.2.19:5000/test1", new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            Toast.makeText(getActivity(), jsonObject.toString(), Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof ServerError) {
                Toast.makeText(getActivity(), new String(((ServerError) error).networkResponse.data, Charset.defaultCharset()), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });
    //        request.form("text", "test " + SystemClock.elapsedRealtime());
    request.setTag("request");
    OkVolley.getInstance().getRequestQueue().add(request);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) ServerError(com.android.volley.ServerError) BaseRequest(im.amomo.volley.sample.BaseRequest)

Example 20 with VolleyError

use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.

the class ThemeBrowserFragment method onMovedToScrapHeap.

@Override
public void onMovedToScrapHeap(View view) {
    // cancel image fetch requests if the view has been moved to recycler.
    NetworkImageView niv = (NetworkImageView) view.findViewById(R.id.theme_grid_item_image);
    if (niv != null) {
        // this tag is set in the ThemeBrowserAdapter class
        String requestUrl = (String) niv.getTag();
        if (requestUrl != null) {
            // need a listener to cancel request, even if the listener does nothing
            ImageContainer container = WordPress.sImageLoader.get(requestUrl, new ImageListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }

                @Override
                public void onResponse(ImageContainer response, boolean isImmediate) {
                }
            });
            container.cancelRequest();
        }
    }
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) NetworkImageView(com.android.volley.toolbox.NetworkImageView) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Aggregations

VolleyError (com.android.volley.VolleyError)180 Response (com.android.volley.Response)121 JSONObject (org.json.JSONObject)104 HashMap (java.util.HashMap)70 JSONException (org.json.JSONException)66 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)61 RequestQueue (com.android.volley.RequestQueue)57 User (model.User)49 JSONArray (org.json.JSONArray)35 CustomRequestArray (dz.easy.androidclient.Util.CustomRequestArray)18 Context (android.content.Context)16 Toast (android.widget.Toast)16 StringRequest (com.android.volley.toolbox.StringRequest)16 RestRequest (com.wordpress.rest.RestRequest)16 CustomRequest (dz.easy.androidclient.Util.CustomRequest)12 TextView (android.widget.TextView)11 MockHttpStack (com.android.volley.mock.MockHttpStack)9 GsonRequest (com.android.volley.toolbox.GsonRequest)9 ImageContainer (com.android.volley.toolbox.ImageLoader.ImageContainer)9 Test (org.junit.Test)9