Volley exception error when response code 304 and 200
Asked Answered
F

1

8

Playing around with the Volley library, I noticed that when making a POST JsonObjectRequest , if the server returns a code 304 or 200 with no data in the response (response.data), Volley interprets it as an error response, instead of a success.

I manage to solve it by adding a couple of lines of code in the method Response<JSONObject> parseNetworkResponse(NetworkResponse response) in the class JsonObjectRequest.java.

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        if (!response.notModified) {// Added for 304 response
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } else // Added for 304 response
            return Response.success(new JSONObject(),HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        Log.v("Volley", "UnsupportedEncodingException " + response.statusCode);
        if (response.statusCode == 200)// Added for 200 response
            return Response.success(new JSONObject(), HttpHeaderParser.parseCacheHeaders(response));
        else
            return Response.error(new ParseError(e));
    } catch (JSONException je) {
        Log.v("Volley", "JSONException " + response.statusCode);
        if (response.statusCode == 200)// Added for 200 response
            return Response.success(new JSONObject(),HttpHeaderParser.parseCacheHeaders(response));
        else
            return Response.error(new ParseError(je));
    }
}

Is it the best solution for this problem?

Thanks!

EDIT

Checking the class BasicNetwork.java I realized that Volley checks if a response has no data by asking if httpResponse.getEntity() != null.

// 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];
    }

But the problem is still the JSONException that occurs when Volley tries to create a new string with response.data == new byte[0] in the parseNetworkResponse method.

Fourway answered 22/2, 2014 at 11:18 Comment(0)
M
6

Miguel- Isn't this method called only if its a success response?

For all status codes <200 or status code >200 volley invokes parseNetworkError(VolleyError volleyError) instead of parseNetworkResponse(NetworkResponse response)method. Look here -

https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/BasicNetwork.java

Line number -118-120

  if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
   }

and the corresponding catch block Line number - 128 -151

catch (IOException e) {
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents,
                        responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
                        statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth",
                            request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError(networkResponse);
            }
        }

If you want to override this behavior you can add your status code specific implementation inside BasicNetwork.java->performRequest method.

Edit : So its not because of status code but because of the empty response. Well I think you are doing the right thing implementing your custom Request class. Volley comes with a few predefined popular types of requests for ease of use, but you can always create your own. Instead of a status code based implementation i'd rather simply check if the following string is empty before deserialzing it -

String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
if (!jsonString .isEmpty()) {
                 return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
}
else {
return Response.success(new JSONObject(),
                    HttpHeaderParser.parseCacheHeaders(response));
}

**haven't tested this, but you get the point :)

Malversation answered 23/2, 2014 at 12:49 Comment(4)
The issue is when the code 200 or the code 304(not modified) return no data, i edited the post.Fourway
what version of volley are you using. the master branch android.googlesource.com/platform/frameworks/volley/+/master/… doesnt have anything that would raise an error in case there's no data in response. Instead they have added a 0 byte response in that case. Line number 106-112Malversation
When you try to create a JSONObject from a String created from a 0 byte object it throw a JSONException.Fourway
Yup its just that your problem isn't the status code but an empty response. So why have an implementation based on status code and not the empty response??Malversation

© 2022 - 2024 — McMap. All rights reserved.