Java Code Examples for com.android.volley.NetworkResponse

The following examples show how to use com.android.volley.NetworkResponse. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: SaveVolley   Source File: StringRequest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    String parsed;
    try {
        /*
         * 1. HttpHeaderParser 解析编码集
         * 2. 将请求结果 Response 的数据 ( data )通过编码集实例化一个数据 ( data ) String 类型
         */
        parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        // 出现编码异常,就不进行编码处理,直接实例化一个 String
        parsed = new String(response.data);
    }
    /*
     * 解析成功,没有异常
     * 1. 将保存好 通过编码解析的 String 数据 返回
     * 2. 再通过 HttpHeaderParser 从网络请求回来的请求结果 NetworkResponse 的 Header 中提取出一个用于缓存的 Cache.Entry
     */
    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 2
Source Project: volley   Source File: HttpHeaderParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void parseCaseInsensitive() {
    long now = System.currentTimeMillis();

    List<Header> headers = new ArrayList<>();
    headers.add(new Header("eTAG", "Yow!"));
    headers.add(new Header("DATE", rfc1123Date(now)));
    headers.add(new Header("expires", rfc1123Date(now + ONE_HOUR_MILLIS)));
    headers.add(new Header("cache-control", "public, max-age=86400"));
    headers.add(new Header("content-type", "text/plain"));

    NetworkResponse response = new NetworkResponse(0, null, false, 0, headers);
    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertEquals("Yow!", entry.etag);
    assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
    assertEquals(entry.softTtl, entry.ttl);
    assertEquals(
            "ISO-8859-1", HttpHeaderParser.parseCharset(HttpHeaderParser.toHeaderMap(headers)));
}
 
Example 3
Source Project: volley   Source File: ResponseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void publicMethods() throws Exception {
    // Catch-all test to find API-breaking changes.
    assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class));
    assertNotNull(Response.class.getMethod("error", VolleyError.class));
    assertNotNull(Response.class.getMethod("isSuccess"));

    assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class));

    assertNotNull(
            Response.ErrorListener.class.getDeclaredMethod(
                    "onErrorResponse", VolleyError.class));

    assertNotNull(
            NetworkResponse.class.getConstructor(
                    int.class, byte[].class, Map.class, boolean.class, long.class));
    assertNotNull(
            NetworkResponse.class.getConstructor(
                    int.class, byte[].class, Map.class, boolean.class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class));
}
 
Example 4
Source Project: volley   Source File: ImageRequestTest.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyResize(
        NetworkResponse networkResponse,
        int maxWidth,
        int maxHeight,
        ScaleType scaleType,
        int expectedWidth,
        int expectedHeight) {
    ImageRequest request =
            new ImageRequest("", null, maxWidth, maxHeight, scaleType, Config.RGB_565, null);
    Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    Bitmap bitmap = response.result;
    assertNotNull(bitmap);
    assertEquals(expectedWidth, bitmap.getWidth());
    assertEquals(expectedHeight, bitmap.getHeight());
}
 
Example 5
Source Project: SaveVolley   Source File: ResponseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test public void publicMethods() throws Exception {
    // Catch-all test to find API-breaking changes.
    assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class));
    assertNotNull(Response.class.getMethod("error", VolleyError.class));
    assertNotNull(Response.class.getMethod("isSuccess"));

    assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class));

    assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse",
            VolleyError.class));

    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class, long.class));
    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class));
}
 
Example 6
Source Project: odyssey   Source File: BulkDownloadService.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void fetchVolleyError(final ArtworkRequestModel model, final Context context, final VolleyError error) {
    if (BuildConfig.DEBUG) {
        Log.e(TAG, "VolleyError for request: " + model.getLoggingString());
    }

    if (error != null) {
        NetworkResponse networkResponse = error.networkResponse;
        if (networkResponse != null && networkResponse.statusCode == 503) {
            finishedLoading();
            return;
        }
    }

    ImageResponse imageResponse = new ImageResponse();
    imageResponse.model = model;
    imageResponse.image = null;
    imageResponse.url = null;
    new InsertImageTask(context, this).execute(imageResponse);
}
 
Example 7
Source Project: VolleyX   Source File: GsonRequest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    if (mType == null && mJavaClass == null) return Response.error(new ParseError());
    try {
        String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        T parsedGSON = null;
        if (mType != null) {
            parsedGSON = mGson.fromJson(jsonString, mType);
        } else {
            parsedGSON = mGson.fromJson(jsonString, mJavaClass);
        }
        return Response.success(parsedGSON,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 8
Source Project: device-database   Source File: ResponseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void publicMethods() throws Exception {
    // Catch-all test to find API-breaking changes.
    assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class));
    assertNotNull(Response.class.getMethod("error", VolleyError.class));
    assertNotNull(Response.class.getMethod("isSuccess"));

    assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class));

    assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse",
            VolleyError.class));

    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class, long.class));
    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class));
}
 
Example 9
Source Project: device-database   Source File: HttpHeaderParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test public void parseCaseInsensitive() {

        long now = System.currentTimeMillis();

        Header[] headersArray = new Header[5];
        headersArray[0] = new BasicHeader("eTAG", "Yow!");
        headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
        headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
        headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
        headersArray[4] = new BasicHeader("content-type", "text/plain");

        Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
        NetworkResponse response = new NetworkResponse(0, null, headers, false);
        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertEquals("Yow!", entry.etag);
        assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
        assertEquals(entry.softTtl, entry.ttl);
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
    }
 
Example 10
Source Project: renrenpay-android   Source File: StringRequestGet.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 11
Source Project: indigenous-android   Source File: Utility.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse network error
 *
 * @param error
 *   The VolleyError
 * @param context
 *   The current context
 * @param network_fail
 *   The string in case of network fail.
 * @param fail
 *   The string in case of general fail.
 */
public static String parseNetworkError(VolleyError error, Context context, int network_fail, int fail) {
    String returnMessage = context.getString(fail);
    try {
        NetworkResponse networkResponse = error.networkResponse;
        if (networkResponse != null && networkResponse.statusCode != 0 && networkResponse.data != null) {
            int code = networkResponse.statusCode;
            String result = new String(networkResponse.data).trim();
            returnMessage = String.format(context.getString(network_fail), code, result);
        }
    }
    catch (Exception ignored) { }

    return returnMessage;
}
 
Example 12
public VolleyMultipartRequest(int method, String url,
                              Response.Listener<NetworkResponse> listener,
                              Response.ErrorListener errorListener) {
    super(method, url, errorListener);
    this.mListener = listener;
    this.mErrorListener = errorListener;
}
 
Example 13
@Override
protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
    try {
        return Response.success(
                response,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (Exception e) {
        return Response.error(new ParseError(e));
    }
}
 
Example 14
Source Project: openapi-generator   Source File: PostRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
  String parsed;
  try {
    parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
  } catch (UnsupportedEncodingException e) {
    parsed = new String(response.data);
  }
  return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 15
Source Project: SaveVolley   Source File: ImageRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
    // Serialize all decode on a global lock to reduce concurrent heap usage.
    // 解析 ImageRequest 的网络请求这块进行加锁,避免 OOM
    synchronized (sDecodeLock) {
        try {
            return doParse(response);
        } catch (OutOfMemoryError e) {
            // 发生 OOM,返回一个只带有 error 的 Response
            VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl());
            return Response.error(new ParseError(e));
        }
    }
}
 
Example 16
Source Project: openapi-generator   Source File: PatchRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
  String parsed;
  try {
    parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
  } catch (UnsupportedEncodingException e) {
    parsed = new String(response.data);
  }
  return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 17
Source Project: Tpay   Source File: StringRequestGet.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 18
Source Project: Tpay   Source File: FastJsonRequest.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Response<BaseMsg> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(
                JSON.parseObject(jsonString, BaseMsg.class), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 19
Source Project: volley   Source File: JsonArrayRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString =
                new String(
                        response.data,
                        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(
                new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 20
Source Project: volley   Source File: StringRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("DefaultCharset")
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    String parsed;
    try {
        parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        // Since minSdkVersion = 8, we can't call
        // new String(response.data, Charset.defaultCharset())
        // So suppress the warning instead.
        parsed = new String(response.data);
    }
    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 21
Source Project: SaveVolley   Source File: JsonArrayRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(new JSONArray(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 22
Source Project: volley   Source File: JsonRequestCharsetTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void defaultCharsetJsonObject() throws Exception {
    // UTF-8 is default charset for JSON
    byte[] data = jsonObjectString().getBytes(Charset.forName("UTF-8"));
    NetworkResponse network = new NetworkResponse(data);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);

    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    assertEquals(TEXT_VALUE, objectResponse.result.getString(TEXT_NAME));
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
 
Example 23
Source Project: SaveVolley   Source File: ImageRequestTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyResize(NetworkResponse networkResponse, int maxWidth, int maxHeight, ScaleType scaleType, int expectedWidth, int expectedHeight) {
    ImageRequest request = new ImageRequest("", null, maxWidth, maxHeight, scaleType,
            Config.RGB_565, null);
    Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    Bitmap bitmap = response.result;
    assertNotNull(bitmap);
    assertEquals(expectedWidth, bitmap.getWidth());
    assertEquals(expectedHeight, bitmap.getHeight());
}
 
Example 24
Source Project: volley   Source File: JsonRequestCharsetTest.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 25
Source Project: SaveVolley   Source File: RequestTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void publicMethods() throws Exception {
    // Catch-all test to find API-breaking changes.
    assertNotNull(Request.class.getConstructor(int.class, String.class,
            Response.ErrorListener.class));

    assertNotNull(Request.class.getMethod("getMethod"));
    assertNotNull(Request.class.getMethod("setTag", Object.class));
    assertNotNull(Request.class.getMethod("getTag"));
    assertNotNull(Request.class.getMethod("getErrorListener"));
    assertNotNull(Request.class.getMethod("getTrafficStatsTag"));
    assertNotNull(Request.class.getMethod("setRetryPolicy", RetryPolicy.class));
    assertNotNull(Request.class.getMethod("addMarker", String.class));
    assertNotNull(Request.class.getDeclaredMethod("finish", String.class));
    assertNotNull(Request.class.getMethod("setRequestQueue", RequestQueue.class));
    assertNotNull(Request.class.getMethod("setSequence", int.class));
    assertNotNull(Request.class.getMethod("getSequence"));
    assertNotNull(Request.class.getMethod("getUrl"));
    assertNotNull(Request.class.getMethod("getCacheKey"));
    assertNotNull(Request.class.getMethod("setCacheEntry", Cache.Entry.class));
    assertNotNull(Request.class.getMethod("getCacheEntry"));
    assertNotNull(Request.class.getMethod("cancel"));
    assertNotNull(Request.class.getMethod("isCanceled"));
    assertNotNull(Request.class.getMethod("getHeaders"));
    assertNotNull(Request.class.getDeclaredMethod("getParams"));
    assertNotNull(Request.class.getDeclaredMethod("getParamsEncoding"));
    assertNotNull(Request.class.getMethod("getBodyContentType"));
    assertNotNull(Request.class.getMethod("getBody"));
    assertNotNull(Request.class.getMethod("setShouldCache", boolean.class));
    assertNotNull(Request.class.getMethod("shouldCache"));
    assertNotNull(Request.class.getMethod("getPriority"));
    assertNotNull(Request.class.getMethod("getTimeoutMs"));
    assertNotNull(Request.class.getMethod("getRetryPolicy"));
    assertNotNull(Request.class.getMethod("markDelivered"));
    assertNotNull(Request.class.getMethod("hasHadResponseDelivered"));
    assertNotNull(
            Request.class.getDeclaredMethod("parseNetworkResponse", NetworkResponse.class));
    assertNotNull(Request.class.getDeclaredMethod("parseNetworkError", VolleyError.class));
    assertNotNull(Request.class.getDeclaredMethod("deliverResponse", Object.class));
    assertNotNull(Request.class.getMethod("deliverError", VolleyError.class));
}
 
Example 26
Source Project: volley   Source File: BasicNetworkTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void notModified() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    List<Header> headers = new ArrayList<>();
    headers.add(new Header("ServerKeyA", "ServerValueA"));
    headers.add(new Header("ServerKeyB", "ServerValueB"));
    headers.add(new Header("SharedKey", "ServerValueShared"));
    headers.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    headers.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    HttpResponse fakeResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_MODIFIED, headers);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.allResponseHeaders = new ArrayList<>();
    entry.allResponseHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    entry.allResponseHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    entry.allResponseHeaders.add(new Header("SharedKey", "CachedValueShared"));
    entry.allResponseHeaders.add(new Header("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1"));
    entry.allResponseHeaders.add(new Header("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2"));
    request.setCacheEntry(entry);
    NetworkResponse response = httpNetwork.performRequest(request);
    List<Header> expectedHeaders = new ArrayList<>();
    // Should have all server headers + cache headers that didn't show up in server response.
    expectedHeaders.add(new Header("ServerKeyA", "ServerValueA"));
    expectedHeaders.add(new Header("ServerKeyB", "ServerValueB"));
    expectedHeaders.add(new Header("SharedKey", "ServerValueShared"));
    expectedHeaders.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    expectedHeaders.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    expectedHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    expectedHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    assertThat(expectedHeaders, containsInAnyOrder(response.allHeaders.toArray(new Header[0])));
}
 
Example 27
Source Project: volley   Source File: BasicNetworkTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void notModified_legacyCache() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    List<Header> headers = new ArrayList<>();
    headers.add(new Header("ServerKeyA", "ServerValueA"));
    headers.add(new Header("ServerKeyB", "ServerValueB"));
    headers.add(new Header("SharedKey", "ServerValueShared"));
    headers.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    headers.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    HttpResponse fakeResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_MODIFIED, headers);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.responseHeaders = new HashMap<>();
    entry.responseHeaders.put("CachedKeyA", "CachedValueA");
    entry.responseHeaders.put("CachedKeyB", "CachedValueB");
    entry.responseHeaders.put("SharedKey", "CachedValueShared");
    entry.responseHeaders.put("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1");
    entry.responseHeaders.put("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2");
    request.setCacheEntry(entry);
    NetworkResponse response = httpNetwork.performRequest(request);
    List<Header> expectedHeaders = new ArrayList<>();
    // Should have all server headers + cache headers that didn't show up in server response.
    expectedHeaders.add(new Header("ServerKeyA", "ServerValueA"));
    expectedHeaders.add(new Header("ServerKeyB", "ServerValueB"));
    expectedHeaders.add(new Header("SharedKey", "ServerValueShared"));
    expectedHeaders.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    expectedHeaders.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    expectedHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    expectedHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    assertThat(expectedHeaders, containsInAnyOrder(response.allHeaders.toArray(new Header[0])));
}
 
Example 28
Source Project: swagger-aem   Source File: PostRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
  String parsed;
  try {
    parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
  } catch (UnsupportedEncodingException e) {
    parsed = new String(response.data);
  }
  return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 29
/**
 * 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();
        }
    }
}
 
Example 30
/**
 * 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();
        }
    }
}