Volley Android library not working with 204 and empty body response

I am using the latest Volley library and I am having problems when my api returns 204 with no response in response. It seems that the following code in BasicNetwork.java does not work 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];
}

getEntity result is never null for me, but it is empty. I made sure my API didn’t return anything by checking curl and postman (just to be sure that I'm not going crazy). Does anyone else have such a problem?

So far, I just changed this if:

if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)

which, as I know, does not solve the root cause, but I want to make sure that I don’t miss anything obvious before diving deeper into this problem.

Thank!

EDIT: , , , - entityToBytes, , .

, API webservice, . - , , .

+4
2

, ! -, 204 API , . BasicNetwork.java - if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)

, JsonObjectRequest, Response.ErrorListener , null.

JsonObjectRequestWithNull, . :

public class JsonObjectRequestWithNull extends JsonRequest<JSONObject> {

public JsonObjectRequestWithNull(int method, String url, JSONObject jsonRequest,
                         Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
            errorListener);
}

public JsonObjectRequestWithNull(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
                         Response.ErrorListener errorListener) {
    this(jsonRequest == null ? Request.Method.GET : Request.Method.POST, url, jsonRequest,
            listener, errorListener);
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        //Allow null
        if (jsonString == null || jsonString.length() == 0) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

}

:

        //Allow null
        if (jsonString == null || jsonString.length() == 0) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }

- .

+5

Volley - HttpStack (gradle dependency: compile 'com.mcxiaoke.volley: library: 1.0.15'), , parseNetworkResponse Request (, Gson Json, GsonRequest, ).

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        final String json = new String(
                response.data, HttpHeaderParser.parseCharset(response.headers));

        if (TextUtils.isEmpty(json)) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }

        return Response.success(gson.fromJson(json, clazz), null);
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}
0

All Articles