What causes the ErrorListener registered to a volley request to be triggered?

saga :

This page says that we can register a Listener to a volley request for handling errors: https://developer.android.com/training/volley/simple

But it doesn't mention what kind of errors trigger this listener. Volley javadoc doesn't say anything about it either.

Specifically, will this listener be executed if a network error occurs. I'm asking this because I've encountered an android code of the following form:

 private void method() {
    String URL = "";
    final int[] status_code = new int[1];
    StringRequest request = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (status_code[0] == 200) {
                        // do something
                    } else {
                        // display toast
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // display toast
                }
            }) {
        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            status_code[0] = response.statusCode;
            return super.parseNetworkResponse(response);
        }
    };
    // add request to queue
}

This code seems to suggest that the registered ErrorListener isn't called for network errors.

What are the conditions which cause the ErrorListener registered to a Volley request to be called

Peng Tuck Kwok :

I'm not exactly privy to your entire code but poking through the Volley source code:

  1. Response.java takes in VolleyError object
  2. VolleyError references NetworkResponses in its use.
  3. Inspecting NetworkResponses deals with mainly HTTP status codes.
  4. Also I'd note that VolleyError extends Exception.

So I would say the call back method is triggered when an Exception is thrown and VolleyError deals with HTTP status codes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93662&siteId=1