How to get status code in successful response Volley Android
Asked Answered
K

3

13

I would like to ask how to get status code when using Volley.

I have following code:

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Here I want to get status code
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
    )

Is it possible ? If not, why ?

Kristoforo answered 12/8, 2014 at 18:37 Comment(0)
K
11

Maybe this class instead of StringResponse:

public class NetworkResponseRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;

public NetworkResponseRequest(int method, String url, Response.Listener<NetworkResponse> listener,
                     Response.ErrorListener errorListener) {
    super(method, url, errorListener);
    mListener = listener;
}

public NetworkResponseRequest(String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {
    this(Method.GET, url, listener, errorListener);
}

@Override
protected void deliverResponse(NetworkResponse response) {
    mListener.onResponse(response);
}

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

public static String parseToString(NetworkResponse response) {
    String parsed;
    try {
        parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        parsed = new String(response.data);
    }
    return parsed;
}
}

Usage:

NetworkResponseRequest request = new NetworkResponseRequest(Request.Method.GET, "http://example.com/test.php",
            new Response.Listener<NetworkResponse>() {
                @Override
                public void onResponse(NetworkResponse response) {
                    // This is status code: response.statusCode
                    // This is string response: NetworkResponseRequest.parseToString(response)
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
    );

It is not nice to use my own implementation instead of StringRequest. What do you think about it ?

Kristoforo answered 13/8, 2014 at 16:51 Comment(1)
This answer really helped me a lot. This answer needs more upvotes!Autophyte
C
4

If you still want to use StringRequest, IMO, you can refer to the following sample code:

public class MainActivity extends AppCompatActivity {

    private final Context mContext = this;        
    private int mStatusCode = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);              

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://google.com";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("onResponse", response);
                Log.i("statusCode", String.valueOf(mStatusCode));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("onErrorResponse", error.toString());
            }
        }) {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                if (response != null) {
                    mStatusCode = response.statusCode;
                }
                return super.parseNetworkResponse(response);
            }
        };

        queue.add(request);
    }
}
Carrollcarronade answered 21/12, 2015 at 2:2 Comment(1)
Thanks for reply BNK. This approach, however, cause repetition of parseNetworkResponse every time you make a different request somewhere in you code. Once you need to change the method in the whole code, you have problem. Moreover, it make no sense to record such a property like status code which belongs to Request object into Activity. After some time, I rather use "MyStringRequest" object based on the solution above which I've posted ;) .Kristoforo
G
-1

Network response can be received in the following format

NetworkResponse response = error.networkResponse;
                if(response != null && response.data != null){
                    switch(response.statusCode){
                        case 403:
                            json = new String(response.data);
                            json = trimMessage(json, "error");
                            if(json != null) displayMessage(json);
                            break;
                    }
                }
Grandam answered 19/12, 2015 at 23:19 Comment(1)
OP asked specifically about successful responseSotos

© 2022 - 2024 — McMap. All rights reserved.