Search in sources :

Example 11 with NetworkResponse

use of com.android.volley.NetworkResponse in project android-volley by mcxiaoke.

the class JsonRequestCharsetTest method specifiedCharsetJsonObject.

@Test
public void specifiedCharsetJsonObject() throws Exception {
    byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=iso-8859-1");
    NetworkResponse network = new NetworkResponse(data, headers);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    //don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) NetworkResponse(com.android.volley.NetworkResponse) String(java.lang.String) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 12 with NetworkResponse

use of com.android.volley.NetworkResponse 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 13 with NetworkResponse

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

the class HttpHeaderParserTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    headers = new HashMap<String, String>();
    response = new NetworkResponse(0, null, headers, false);
}
Also used : NetworkResponse(com.android.volley.NetworkResponse)

Example 14 with NetworkResponse

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

the class StatsUtils method logVolleyErrorDetails.

public static synchronized void logVolleyErrorDetails(final VolleyError volleyError) {
    if (volleyError == null) {
        AppLog.e(T.STATS, "Tried to log a VolleyError, but the error obj was null!");
        return;
    }
    if (volleyError.networkResponse != null) {
        NetworkResponse networkResponse = volleyError.networkResponse;
        AppLog.e(T.STATS, "Network status code: " + networkResponse.statusCode);
        if (networkResponse.data != null) {
            AppLog.e(T.STATS, "Network data: " + new String(networkResponse.data));
        }
    }
    AppLog.e(T.STATS, "Volley Error Message: " + volleyError.getMessage(), volleyError);
}
Also used : NetworkResponse(com.android.volley.NetworkResponse)

Example 15 with NetworkResponse

use of com.android.volley.NetworkResponse in project android_frameworks_base by AOSPA.

the class URLFetcher method getExpirationTimeMillisFromHTTPHeader.

/**
     * Parses the HTTP headers to compute the ttl.
     *
     * @param headers a map that map the header key to the header values. Can be null.
     * @return the ttl in millisecond or null if the ttl is not specified in the header.
     */
private Long getExpirationTimeMillisFromHTTPHeader(Map<String, List<String>> headers) {
    if (headers == null) {
        return null;
    }
    Map<String, String> joinedHeaders = joinHttpHeaders(headers);
    NetworkResponse response = new NetworkResponse(null, joinedHeaders);
    Cache.Entry cachePolicy = HttpHeaderParser.parseCacheHeaders(response);
    if (cachePolicy == null) {
        // Cache is disabled, set the expire time to 0.
        return DO_NOT_CACHE_RESULT;
    } else if (cachePolicy.ttl == 0) {
        // Cache policy is not specified, set the expire time to 0.
        return DO_NOT_CACHE_RESULT;
    } else {
        // cachePolicy.ttl is actually the expire timestamp in millisecond.
        return cachePolicy.ttl;
    }
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache)

Aggregations

NetworkResponse (com.android.volley.NetworkResponse)46 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Cache (com.android.volley.Cache)9 JSONObject (org.json.JSONObject)9 AuthFailureError (com.android.volley.AuthFailureError)8 NetworkError (com.android.volley.NetworkError)8 NoConnectionError (com.android.volley.NoConnectionError)8 ServerError (com.android.volley.ServerError)8 TimeoutError (com.android.volley.TimeoutError)8 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)8 JSONArray (org.json.JSONArray)8 JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)7 HttpResponse (org.apache.http.HttpResponse)7 StatusLine (org.apache.http.StatusLine)7 String (java.lang.String)6