Q:

Handle an empty response in a JSONRequest with Volley

I'm using Volley to make a POST request in my application, and in my case, a good response is a 201 with an empty body. I'm using a JSONRequest, to make the call.
My problem is that the error response handler is getting called because response is empty. 
Below is my request:
    Request request = new JsonRequest<Object>(Request.Method.POST, url, body, new Response.Listener<Object>() {

        @Override
        public void onResponse(Object response) {

        }
    }, new ErrorListener(context)) {

        @Override
        protected Response<Object> parseNetworkResponse(NetworkResponse response) {

            Log.d(TAG, "success!!!!!!");
            if (response.statusCode == 201)
                mListener.resetPasswordWasSent();
            return null;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/json");
            params.put("Accept", "application/json");
            return params;
        }
    };

    requestQueue.add(request);

My parseNetworkResponse function is getting called, then the ErrorListener, and the onResponse method never gets hits because I get a NullPointerException in the ErrorListener.
I can ignore the NullPointerException in my error listener, but I'd prefer not to. Obviously, I can simply send my callback in the parseNetworkResponse, but I don't want to have any errors popping up. 
Anyone know how I should handle this?
Edit:
Here is the stacktrace:
05-06 09:44:19.586  27546-27560/com.threepoundhealth.euco E/Volley﹕ [1830] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
    java.lang.NullPointerException
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:126)

A:

You could try to hack like this. Create a JsonObjectRequest subclass, override the parseNetworkResponse method and check the response data, if it is an empty byte[],
replace the data with byte[] representation of a empty json {}.
public class VolleyJsonRequest extends JsonObjectRequest {

    ...

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            if (response.data.length == 0) {
                byte[] responseData = "{}".getBytes("UTF8");
                response = new NetworkResponse(response.statusCode, responseData, response.headers, response.notModified);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return super.parseNetworkResponse(response);
    }    
}

A:

You can use a StringRequest. Your listener will be called with an empty String "". 

