Search in sources :

Example 21 with ServerError

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

the class OkNetwork method performRequest.

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        Response httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        try {
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            int statusCode = httpResponse.code();
            responseHeaders = new TreeMap<String, String>();
            for (String field : httpResponse.headers().names()) {
                responseHeaders.put(field, httpResponse.headers().get(field));
            }
            // Handle cache validation.
            if (statusCode == 304) {
                return new NetworkResponse(304, request.getCacheEntry().data, responseHeaders, true);
            }
            if (httpResponse.body() != null) {
                if (responseGzip(responseHeaders)) {
                    Buffer buffer = new Buffer();
                    GzipSource gzipSource = new GzipSource(httpResponse.body().source());
                    while (gzipSource.read(buffer, Integer.MAX_VALUE) != -1) {
                    }
                    responseContents = buffer.readByteArray();
                } else {
                    responseContents = httpResponse.body().bytes();
                }
            } else {
                responseContents = new byte[0];
            }
            // // Some responses such as 204s do not have content.  We must check.
            // if (httpResponse.getEntity() != null) {
            // responseContents = entityToBytes(httpResponse.getEntity()
            // , responseGzip(responseHeaders));
            // } else {
            // // Add 0 byte response as a way of honestly representing a
            // // no-content request.
            // responseContents = new byte[0];
            // }
            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, responseContents, httpResponse);
            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            int statusCode;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.code();
            } else {
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
                if (statusCode == 401 || statusCode == 403) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError(networkResponse);
            }
        }
    }
}
Also used : Buffer(okio.Buffer) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) Response(com.squareup.okhttp.Response) NetworkResponse(com.android.volley.NetworkResponse) GzipSource(okio.GzipSource) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 22 with ServerError

use of com.android.volley.ServerError in project Saiy-PS by brandall76.

the class BingTranslateAPI method verboseError.

/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {
    final NetworkResponse response = error.networkResponse;
    if (response != null && error instanceof ServerError) {
        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ServerError(com.android.volley.ServerError) NetworkResponse(com.android.volley.NetworkResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UtilsString(ai.saiy.android.utils.UtilsString)

Example 23 with ServerError

use of com.android.volley.ServerError in project Saiy-PS by brandall76.

the class DeleteIDProfile method verboseError.

/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {
    final NetworkResponse response = error.networkResponse;
    if (response != null && error instanceof ServerError) {
        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ServerError(com.android.volley.ServerError) NetworkResponse(com.android.volley.NetworkResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with ServerError

use of com.android.volley.ServerError in project Saiy-PS by brandall76.

the class BVAuthRequest method verboseError.

/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {
    final NetworkResponse response = error.networkResponse;
    if (response != null && error instanceof ServerError) {
        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ServerError(com.android.volley.ServerError) NetworkResponse(com.android.volley.NetworkResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 25 with ServerError

use of com.android.volley.ServerError in project Saiy-PS by brandall76.

the class BingOAuth method verboseError.

/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(final VolleyError error) {
    final NetworkResponse response = error.networkResponse;
    if (response != null && error instanceof ServerError) {
        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ServerError(com.android.volley.ServerError) NetworkResponse(com.android.volley.NetworkResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ServerError (com.android.volley.ServerError)28 NetworkResponse (com.android.volley.NetworkResponse)19 IOException (java.io.IOException)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 AuthFailureError (com.android.volley.AuthFailureError)9 NetworkError (com.android.volley.NetworkError)9 NoConnectionError (com.android.volley.NoConnectionError)9 TimeoutError (com.android.volley.TimeoutError)9 MalformedURLException (java.net.MalformedURLException)9 SocketTimeoutException (java.net.SocketTimeoutException)9 HashMap (java.util.HashMap)9 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)9 InputStream (java.io.InputStream)8 HttpResponse (org.apache.http.HttpResponse)8 StatusLine (org.apache.http.StatusLine)8 Entry (com.android.volley.Cache.Entry)5 ClientError (com.android.volley.ClientError)2 UtilsString (ai.saiy.android.utils.UtilsString)1 Cache (com.android.volley.Cache)1 RedirectError (com.android.volley.RedirectError)1