I'm using the latest Volley library and I'm having issues when my api is returning a 204 with no body in the response. It seems that the following code in BasicNetwork.java isn't working as expected:
// 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];
}
the result from getEntity is never null for me, but it is empty. I've made sure that my API is returning nothing by checking curl and postman (just to be extra sure i'm not going crazy). Has anyone else has such an issue?
For now I've just changed that if to:
if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)
which I know isn't solving the root cause, but I want to make sure that I'm not missing anything obvious before diving deeper into this problem.
Thanks!
EDIT: Sorry, I forgot to mention that the actual problem was that a timeout exception occurs whilst in the method entityToBytes, which is strange since there is no body to retrieve.
Also, i'm not using an actual fully functioning webservice API, as it's not available yet. Instead i'm connecting to a mocked out webservice on apiary, but i don't see how apiary could be the problem.
This is less an answer than an elaboration on your solution! First off, I use 204 responses from my API and had the exact same issue you had. I used your code in BasicNetwork.java to solve it - the line
if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)
What I also found was that if I use a standard
JsonObjectRequest
request then theResponse.ErrorListener
would be triggered because the body was null.I created a new
JsonObjectRequestWithNull
which provides a success response in the event of a null or blank body. Code:}
The relevant bit is:
Hope helpful to someone.