Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 985
coder Avatar asked May 06 '14 13:05

coder


People also ask

How do I get volley response error?

If you want to view the data in the case of an error code in response, you can use something like this: @Override public void onErrorResponse(VolleyError error) { String body; //get status code here String statusCode = String. valueOf(error. networkResponse.

How can I call multiple requests at the same time in volley?

1 Answer. Show activity on this post. To achieve it without using any patterns or other libraries, you can mark the request as finished if it responded, and call the method, in each of them, you want to execute if all the requests are finished. On that method, you just need to check if all the requests are done.

What is volley Kotlin?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.


1 Answers

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);
    }    
}
like image 64
Fantasy Avatar answered Sep 25 '22 04:09

Fantasy