b2cloud

6th June 2014

Getting a server response status 200 from Android Volley library

Thoughts | Tutorial By 2 years ago

I use Android Volley library whenever there is a requirement for web requests in my projects. It is flawless when it comes to make JSON or normal String request to the web server but whenever you make one of these request, you must expect the server response in the same format else you request will fail for ex. if i use JSONObjectRequest, it means i am expecting JSON from the server as a response and if server does not return the JSON in response, i’ll get the JSONParseException. But there are situations when i need to send JSON to the server and in response, i am expecting nothing but only the status code (normally 200 OK) from server indicating that request is valid or successful.

I have been in this situation few times so i decided to write a custom class that accept the String or JSON but in response it’ll give me only the server response status code indicating that my request was successful.

Here is the code for the class –

   
    public class ServerStatusRequestObject extends Request {

    private final Response.Listener mListener;
    private String mBody;
    private String mContentType;
    private HashMap mCustomHeaders;

    public ServerStatusRequestObject(int method,
                                     String url,
                                     HashMap customHeaders,
                                     String body,
                                     Response.Listener listener,
                                     Response.ErrorListener errorListener) {

        super(method, url, errorListener);
        mCustomHeaders = customHeaders;
        mBody = body;
        mListener = listener;
        mContentType = "application/json";

        if (method == Method.POST) {
            RetryPolicy policy = new DefaultRetryPolicy(5000, 0, 5);
            setRetryPolicy(policy);
        }
    }

    public ServerStatusRequestObject(String url,
                                     HashMap customHeaders,
                                     Response.Listener listener,
                                     Response.ErrorListener errorListener) {

        super(Method.GET, url, errorListener);
        mCustomHeaders = customHeaders;
        mListener = listener;
        mContentType = "application/x-www-form-urlencoded";
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        return Response.success(response.statusCode, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(Integer statusCode) {
        mListener.onResponse(statusCode);
    }

    @Override
    public Map getHeaders() throws AuthFailureError {
        if (mCustomHeaders != null) {
            return mCustomHeaders;
        }
        return super.getHeaders();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return mBody.getBytes();
    }    

    @Override
    public String getBodyContentType() {
        return mContentType;
    }

    public String getContentType() {
        return mContentType;
    }

    public void setContentType(String mContentType) {
        this.mContentType = mContentType;
    }
}

Steps and brief explanation –

1. Create a class that extends volley’s Request class and must set the type to Integer
2. Override parseNetworkResponse, deliverResponse method in order to return the status code
3. In parseNetworkResponse return response.statusCode as the first parameter in a static function
called success of Request class and second parameter has to be the headers of the response
4. parseNetworkResponse will parse the result and eventually our deliverResponse method will get called afterwards
5. Now in deliverResponse just return the integer value to your listener. This integer value is our server response status code

That’s it, we are done.

If you need to send JSONObject as a part of request then you can pass the JSONObject.toString() to the constructor and it should just work.

Also, You can set your own content type by calling setContentType() method of this class.

Hope it helps.

  • Muhammed Rajeef M K

    Hi Chandan,

    Thanks for the post. I’ve hit on an error when following the instruction.

    When I override deliverReponse method, this is the default one

    @Override
    protected void deliverResponse(Object reponse) {
    //TODO Auto-generated method stub
    }

    instead of

    @Override
    protected void deliverResponse(Integer statusCode) {
    mListener.onResponse(statusCode);

    }

    Any idea how to to go forward ?

    • Muhammed Rajeef M K

      [Solution] Ok. Anybody facing the same issue, you need to extend your class with Request instead of Request alone. This will enable you to override the default deliverResponse method with Integer/statusCode as its argument.

Recommended Posts

Write JNI wrappers for Android

Post by 2 years ago

Following are the steps that you need to follow┬áin order to make things working – 1. Setting up the NDK on my machine 2. Learn how to write JNI wrappers 3. Learn C++ briefly so

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!