@Test public void testParseNetworkResponseWithBadJson() { final Map<String, String> headers = new HashMap<>(); headers.put(CONTENT_TYPE, "application/javascript; charset=" + CHARSET); try { final NetworkResponse response = new NetworkResponse("{test: [1,2}".getBytes(CHARSET), headers); final Response<Object> parsedResponse = decorator.parseNetworkResponse(response); assertFalse("The request has failed", parsedResponse.isSuccess()); } catch (final UnsupportedEncodingException e) { fail(e.getMessage()); } }
@Override protected Response<T> parseNetworkResponse(final NetworkResponse response) { try { final String headersCharset = HttpHeaderParser.parseCharset(response.headers); final String json = new String(response.data, headersCharset); final JSONObject object = new JSONObject(json); final JSONArray array = object.getJSONArray(elementsKey); final byte[] data = array.toString().getBytes(headersCharset); final NetworkResponse responseJsonArray = new NetworkResponse(data, response.headers); return super.parseNetworkResponse(responseJsonArray); } catch (final UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (final JSONException e) { return Response.error(new ParseError(e)); } }
JSONArrayRequestDecorator extends RequestDecorator<T> { @Override protected Response<T> parseNetworkResponse(final NetworkResponse response) { try { final String headersCharset = HttpHeaderParser.parseCharset(response.headers); final String json = new String(response.data, headersCharset); final JSONObject object = new JSONObject(json); final JSONArray array = object.getJSONArray(elementsKey); final byte[] data = array.toString().getBytes(headersCharset); final NetworkResponse responseJsonArray = new NetworkResponse(data, response.headers); return super.parseNetworkResponse(responseJsonArray); } catch (final UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (final JSONException e) { return Response.error(new ParseError(e)); } } }
JSONArrayRequestDecorator extends RequestDecorator<T> { @Override protected Response<T> parseNetworkResponse(final NetworkResponse response) { try { final String headersCharset = HttpHeaderParser.parseCharset(response.headers); final String json = new String(response.data, headersCharset); final JSONObject object = new JSONObject(json); final JSONArray array = object.getJSONArray(elementsKey); final byte[] data = array.toString().getBytes(headersCharset); final NetworkResponse responseJsonArray = new NetworkResponse(data, response.headers); return super.parseNetworkResponse(responseJsonArray); } catch (final UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (final JSONException e) { return Response.error(new ParseError(e)); } } JSONArrayRequestDecorator(@NonNull final Request<T> request, @NonNull final String elementsKey); }
JSONArrayRequestDecorator extends RequestDecorator<T> { @Override protected Response<T> parseNetworkResponse(final NetworkResponse response) { try { final String headersCharset = HttpHeaderParser.parseCharset(response.headers); final String json = new String(response.data, headersCharset); final JSONObject object = new JSONObject(json); final JSONArray array = object.getJSONArray(elementsKey); final byte[] data = array.toString().getBytes(headersCharset); final NetworkResponse responseJsonArray = new NetworkResponse(data, response.headers); return super.parseNetworkResponse(responseJsonArray); } catch (final UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (final JSONException e) { return Response.error(new ParseError(e)); } } JSONArrayRequestDecorator(@NonNull final Request<T> request, @NonNull final String elementsKey); @NonNull String getElementsKey(); @Override String toString(); }
JSONArrayRequestDecorator extends RequestDecorator<T> { @Override protected Response<T> parseNetworkResponse(final NetworkResponse response) { try { final String headersCharset = HttpHeaderParser.parseCharset(response.headers); final String json = new String(response.data, headersCharset); final JSONObject object = new JSONObject(json); final JSONArray array = object.getJSONArray(elementsKey); final byte[] data = array.toString().getBytes(headersCharset); final NetworkResponse responseJsonArray = new NetworkResponse(data, response.headers); return super.parseNetworkResponse(responseJsonArray); } catch (final UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (final JSONException e) { return Response.error(new ParseError(e)); } } JSONArrayRequestDecorator(@NonNull final Request<T> request, @NonNull final String elementsKey); @NonNull String getElementsKey(); @Override String toString();  }