use of com.android.volley.NetworkResponse in project FastDev4Android by jiangqqlmj.
the class JsonRequestCharsetTest method defaultCharsetJsonArray.
@Test
public void defaultCharsetJsonArray() throws Exception {
// UTF-8 is default charset for JSON
byte[] data = jsonArrayString().getBytes(Charset.forName("UTF-8"));
NetworkResponse network = new NetworkResponse(data);
JsonArrayRequest arrayRequest = new JsonArrayRequest("", null, null);
Response<JSONArray> arrayResponse = arrayRequest.parseNetworkResponse(network);
assertNotNull(arrayResponse);
assertTrue(arrayResponse.isSuccess());
assertEquals(TEXT_VALUE, arrayResponse.result.getString(TEXT_INDEX));
assertEquals(COPY_VALUE, arrayResponse.result.getString(COPY_INDEX));
}
use of com.android.volley.NetworkResponse in project FastDev4Android by jiangqqlmj.
the class BasicNetworkTest method headersAndPostParams.
@Test
public void headersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of com.android.volley.NetworkResponse in project android-volley by mcxiaoke.
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 = Collections.emptyMap();
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) {
Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// A HTTP 304 response does not have all header fields. We
// have to use the header fields from the cache entry plus
// the new ones from the response.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
entry.responseHeaders.putAll(responseHeaders);
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// Handle moved resources
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
String newUrl = responseHeaders.get("Location");
request.setRedirectUrl(newUrl);
}
// 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, SystemClock.elapsedRealtime() - requestStart);
} 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);
}
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
VolleyLog.e("Request at %s has been redirected to %s", request.getOriginUrl(), request.getUrl());
} else {
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
}
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
} else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
attemptRetryOnException("redirect", request, new RedirectError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(e);
}
}
}
}
use of com.android.volley.NetworkResponse in project android-volley by mcxiaoke.
the class HttpHeaderParserTest method setUp.
@Before
public void setUp() throws Exception {
headers = new HashMap<String, String>();
response = new NetworkResponse(0, null, headers, false);
}
use of com.android.volley.NetworkResponse in project android-volley by mcxiaoke.
the class ImageRequestTest method parseNetworkResponse_resizing.
@Test
public void parseNetworkResponse_resizing() throws Exception {
// This is a horrible hack but Robolectric doesn't have a way to provide
// width and height hints for decodeByteArray. It works because the byte array
// "file:fake" is ASCII encodable and thus the name in Robolectric's fake
// bitmap creator survives as-is, and provideWidthAndHeightHints puts
// "file:" + name in its lookaside map. I write all this because it will
// probably break mysteriously at some point and I feel terrible about your
// having to debug it.
byte[] jpegBytes = "file:fake".getBytes();
ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
NetworkResponse jpeg = new NetworkResponse(jpegBytes);
// Scale the image uniformly (maintain the image's aspect ratio) so that
// both dimensions (width and height) of the image will be equal to or
// less than the corresponding dimension of the view.
ScaleType scalteType = ScaleType.CENTER_INSIDE;
// Exact sizes
// exactly half
verifyResize(jpeg, 512, 250, scalteType, 512, 250);
// just under half
verifyResize(jpeg, 511, 249, scalteType, 509, 249);
// larger
verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
// keep same ratio
verifyResize(jpeg, 500, 500, scalteType, 500, 244);
// Specify only width, preserve aspect ratio
verifyResize(jpeg, 512, 0, scalteType, 512, 250);
verifyResize(jpeg, 800, 0, scalteType, 800, 390);
verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
// Specify only height, preserve aspect ratio
verifyResize(jpeg, 0, 250, scalteType, 512, 250);
verifyResize(jpeg, 0, 391, scalteType, 800, 391);
verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
// No resize
verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
// Scale the image uniformly (maintain the image's aspect ratio) so that
// both dimensions (width and height) of the image will be equal to or
// larger than the corresponding dimension of the view.
scalteType = ScaleType.CENTER_CROP;
// Exact sizes
verifyResize(jpeg, 512, 250, scalteType, 512, 250);
verifyResize(jpeg, 511, 249, scalteType, 511, 249);
verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
verifyResize(jpeg, 500, 500, scalteType, 1024, 500);
// Specify only width
verifyResize(jpeg, 512, 0, scalteType, 512, 250);
verifyResize(jpeg, 800, 0, scalteType, 800, 390);
verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
// Specify only height
verifyResize(jpeg, 0, 250, scalteType, 512, 250);
verifyResize(jpeg, 0, 391, scalteType, 800, 391);
verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
// No resize
verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
// Scale in X and Y independently, so that src matches dst exactly. This
// may change the aspect ratio of the src.
scalteType = ScaleType.FIT_XY;
// Exact sizes
verifyResize(jpeg, 512, 250, scalteType, 512, 250);
verifyResize(jpeg, 511, 249, scalteType, 511, 249);
verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
verifyResize(jpeg, 500, 500, scalteType, 500, 500);
// Specify only width
verifyResize(jpeg, 512, 0, scalteType, 512, 500);
verifyResize(jpeg, 800, 0, scalteType, 800, 500);
verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
// Specify only height
verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
// No resize
verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
Aggregations