Search in sources :

Example 1 with ServerError

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

the class BasicNetwork method performRequest.

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        try {
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // Handle cache validation.
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry() == null ? null : request.getCacheEntry().data, responseHeaders, true);
            }
            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                responseContents = entityToBytes(httpResponse.getEntity());
            } 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, statusLine);
            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 = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } 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 == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    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 : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) HttpResponse(org.apache.http.HttpResponse) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) StatusLine(org.apache.http.StatusLine) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 2 with ServerError

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

the class BasicNetwork method entityToBytes.

/**
 * Reads the contents of HttpEntity into a byte[].
 */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
Also used : InputStream(java.io.InputStream) ServerError(com.android.volley.ServerError) IOException(java.io.IOException)

Example 3 with ServerError

use of com.android.volley.ServerError in project AndroidLife by CaMnter.

the class BasicNetwork method entityToBytes.

/**
 * Reads the contents of HttpEntity into a byte[].
 */
/*
     * 执行 Apache HttpEntity -> byte[] 的转化
     */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    /*
         * 实例化一个 PoolingByteArrayOutputStream 对象
         *
         * PoolingByteArrayOutputStream 内设置了一个 ByteArrayPool,会将一些 实例化好的 byte[] 进行缓存
         * 然后回收利用,提供读取流数据
         *
         * 将当前 BasicNetwork 的 ByteArrayPool 提供 给 PoolingByteArrayOutputStream
         * 还需要将 传入一个 需要 output 的数据长度 给 PoolingByteArrayOutputStream,这里用
         * (int) entity.getContentLength() 进行获取
         */
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        // 获取 Apache HttpEntity 的 content 数据流
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        // 获取一个 byte[].length = 1024 的 byte[]
        buffer = mPool.getBuf(1024);
        int count;
        // 开始 边读边写
        while ((count = in.read(buffer)) != -1) {
            // 写入到 PoolingByteArrayOutputStream 中
            bytes.write(buffer, 0, count);
        }
        // 从 PoolingByteArrayOutputStream 中取出 byte[] 数据
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            // 调用 Apache API 关闭 Apache HttpEntity 的 content 数据流
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        // 将使用的 byte[] 放入 ByteArrayPool 缓存回收
        mPool.returnBuf(buffer);
        // 关闭 PoolingByteArrayOutputStream 流
        bytes.close();
    }
}
Also used : InputStream(java.io.InputStream) ServerError(com.android.volley.ServerError) IOException(java.io.IOException)

Example 4 with ServerError

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

the class CreateIDProfile 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 5 with ServerError

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

the class FetchIDOperation 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)

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