SimpleXmlRequest extends AbstractConverterRequest<T> { @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { String charset = HttpHeaderParser.parseCharset(response.headers); Reader reader = null; try { reader = new InputStreamReader(new ByteArrayInputStream(response.data), charset); T result = persister.read(getTargetClass(), reader); return Response.success(result, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (XmlPullParserException e) { return Response.error(new ParseError(e)); } catch (ElementException e) { return Response.error(new ParseError(e)); } catch (Exception e) { return Response.error(new VolleyError(e)); } finally { IoUtils.closeQuietly(reader); } } SimpleXmlRequest(String url, Class<T> clazz, Listener<T> listener);  SimpleXmlRequest(String url, Class<T> clazz, Persister persister, Listener<T> listener);  SimpleXmlRequest(String url, Class<T> clazz, Listener<T> listener,
			ErrorListener errorListener);  SimpleXmlRequest(String url, Class<T> clazz, Persister persister, Listener<T> listener, 
			ErrorListener errorListener);  SimpleXmlRequest(int method, String url, Class<T> clazz,
			Listener<T> listener, ErrorListener errorListener);  SimpleXmlRequest(int method, String url, Class<T> clazz, Persister persister,
			Listener<T> listener, ErrorListener errorListener);   }
@Test public void networkResponseShouldBeParsed() throws Exception { String content = "<news>\n" + " <imageUrl>http: " <title>Volley extention has released</title>\n" + "</news>"; SimpleXmlRequest<News> request = new SimpleXmlRequest<News>(url, News.class,listener); NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); 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 networkResponseShouldNotBeParsedWithUnsupportedException() throws Exception { String content = "<news>\n" + " <imageUrl>http: " <title>Volley extention has released</title>\n" + "</news>"; HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HTTP.CONTENT_TYPE, "text/html;charset=UTF-14"); SimpleXmlRequest<News> request = new SimpleXmlRequest<News>(url, News.class,listener); NetworkResponse networkResponse = new NetworkResponse(content.getBytes(), headers); Response<News> response = request.parseNetworkResponse(networkResponse); assertNull(response.result); assertThat(response.error, is(instanceOf(ParseError.class))); assertThat(response.error.getCause(), is(instanceOf(UnsupportedEncodingException.class))); }
@Ignore @Test public void networkResponseShouldNotBeParsedWithInvalidFormat() throws JsonProcessingException { String content = "<news>\n" + " <imageUrl>http: " <title>Volley extention has released</title>\n" + "</news----->"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); SimpleXmlRequest<News> request = new SimpleXmlRequest<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(XMLStreamException.class))); }
@Test public void networkResponseShouldNotBeParsedWithAddtionalTag() throws JsonProcessingException { String content = "<news>\n" + " <imageUrl>http: " <title>Volley extention has released</title>\n" + " <summary>Good news</summary>\n" + "</news>"; NetworkResponse networkResponse = new NetworkResponse(content.getBytes()); SimpleXmlRequest<News> request = new SimpleXmlRequest<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(ElementException.class))); }