简体   繁体   中英

How to catch unexpected response errors from Volley

I am using Volley to access some REST API. If a user enters a wrong password or anything else goes wrong I want to catch the exception. Eg:

E/Volley: [281] BasicNetwork.performRequest: Unexpected response code 401

Can I catch this expception somehow?

Here is the LoginRequest:

public class LoginRequest  extends StringRequest{
    private static final String LOGIN_REQUEST_URL = "URL";
    public static final String CLIENT_KEY= "SECRET";
    public static final String CLIENT_ID = "ID";
    private Map<String, String> params;

    public LoginRequest(String username, String password, Response.Listener<String> listener) {
        super(Method.POST, LOGIN_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("client_secret", CLIENT_KEY);
        params.put("client_id", CLIENT_ID);
        params.put("grant_type", "password");
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

Solution:

I added:

public class LoginRequest  extends StringRequest{
    private static final String LOGIN_REQUEST_URL = "URL";
    public static final String CLIENT_KEY= "KEY";
    public static final String CLIENT_ID = "ID";
    private Map<String, String> params;

    public LoginRequest(String username, String password, Response.Listener<String> listener, **Response.ErrorListener errorListener**) {
        super(Method.POST, LOGIN_REQUEST_URL, listener, **errorListener**);
        params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("client_secret", CLIENT_KEY);
        params.put("client_id", CLIENT_ID);
        params.put("grant_type", "password");
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

And added an Resonse.ErrorListener to my LoginActiviy and called the LoginRequest Method with the additional Parameter.

To catch all errors in volley, onErrorResponse is not reliable enough, I sometimes get null in onErrorResponse. I prefer to override deliverError like this:

 @Override
    public void deliverError(VolleyError error){
        Log.e("deliverError", " here");
       int status = error.networkResponse.statusCode;

        switch (status)
        {
            case 401:

                break;
            case 307:
                break;
            ...
            default:

        }
    }

You can create a custom error listener which implements Volley's Response.ErrorListener , override onErrorResponse() method and get the response code.

EDIT:

Actually you not need to create a custom ErrorListener since the default Volley's ErrorListener will be sufficient for your needs.

Just pass an instance of the Response.ErrorListener to your LoginRequest and do your logic in the onErrorResponse() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM