JacksonRequest extends AbstractConverterRequest<T> { @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { T result = objectMapper.readValue(getBodyString(response), getTargetClass()); return Response.success(result, HttpHeaderParser.parseCacheHeaders(response)); } catch (JsonParseException e) { return Response.error(new ParseError(e)); } catch (JsonMappingException e) { return Response.error(new ParseError(e)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (IOException e) { return Response.error(new VolleyError(e)); } catch (Exception e) { return Response.error(new VolleyError(e)); } } JacksonRequest(String url, Class<T> clazz, Listener<T> listener);  JacksonRequest(String url, Class<T> clazz, ObjectMapper objectMapper, Listener<T> listener);  JacksonRequest(String url, Class<T> clazz, Listener<T> listener,
			ErrorListener errorListener);  JacksonRequest(String url, Class<T> clazz, ObjectMapper objectMapper, Listener<T> listener, ErrorListener errorListener);  JacksonRequest(int method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener);  JacksonRequest(int method, String url, Class<T> clazz, ObjectMapper objectMapper, Listener<T> listener, ErrorListener errorListener);   }
@Test public void networkResponseShouldBeParsed() throws JsonProcessingException { String content = "{\"imageUrl\":\"http: "\"title\":\"Volley extention has released\"}"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, listener); Response<News> response = request.parseNetworkResponse(networkResponse); News news = response.result; assertThat(news.imageUrl, is("http: assertThat(news.title, is("Volley extention has released")); }
@Test public void networkResponseShouldBeParsedWithAddtionalAttribute() throws JsonProcessingException { String content = "{\"imageUrl\":\"http: "\"title\":\"Volley extention has released\"," + "\"content\":\"Very good News\"}"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, listener); Response<News> response = request.parseNetworkResponse(networkResponse); News news = response.result; assertThat(news.imageUrl, is("http: assertThat(news.title, is("Volley extention has released")); }
@Test public void networkResponseShouldReturnErrorWithAddtionalAttribute() throws JsonProcessingException { String content = "{\"imageUrl\":\"http: "\"title\":\"Volley extention has released\"," + "\"content\":\"Very good News\"}"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); ObjectMapper objectMapperWithDefaultOption = new ObjectMapper(); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, objectMapperWithDefaultOption, listener); Response<News> response = request.parseNetworkResponse(networkResponse); assertNull(response.result); assertThat(response.error, is(instanceOf(ParseError.class))); assertThat(response.error.getCause(), is(instanceOf(UnrecognizedPropertyException.class))); }
@Test public void networkResponseShouldNotBeParsedWithInvalidFormat() throws JsonProcessingException { String content = "{\"imageUrl\":\"http: NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, listener); Response<News> response = request.parseNetworkResponse(networkResponse); assertNull(response.result); assertThat(response.error, is(instanceOf(ParseError.class))); assertThat(response.error.getCause(), is(instanceOf(JsonParseException.class))); }
@Test public void networkResponseShouldBeParsedWithSpecialChars() throws JsonProcessingException { String content = "{\"title\":\"å &acirc;\"}"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); ObjectMapper objectMapperWithDefaultOption = new ObjectMapper(); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, objectMapperWithDefaultOption, listener); Response<News> response = request.parseNetworkResponse(networkResponse); assertNotNull(response.result); assertNull(response.error); }
@Test public void networkResponseShouldNotBeParsedWithUnsupportedException() throws JsonProcessingException { String content = "{\"imageUrl\":\"http: HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HTTP.CONTENT_TYPE, "text/html;charset=UTF-14"); NetworkResponse networkResponse = new NetworkResponse(content.getBytes(), headers); JacksonRequest<News> request = new JacksonRequest<News>(url, News.class, listener); Response<News> response = request.parseNetworkResponse(networkResponse); assertNull(response.result); assertThat(response.error, is(instanceOf(ParseError.class))); assertThat(response.error.getCause(), is(instanceOf(UnsupportedEncodingException.class))); }