[英]How to catch unexpected response errors from Volley
I am using Volley to access some REST API. 我正在使用Volley访问某些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: 这是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. 并将Resonse.ErrorListener添加到我的LoginActiviy中,并使用其他参数调用LoginRequest方法。
To catch all errors in volley, onErrorResponse is not reliable enough, I sometimes get null in onErrorResponse. 为了捕获齐射中的所有错误,onErrorResponse不够可靠,有时我在onErrorResponse中得到null。 I prefer to override deliverError like this: 我更喜欢这样覆盖liverError :
@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. 您可以创建一个实现Volley的Response.ErrorListener的自定义错误侦听器,重写onErrorResponse()方法并获取响应代码。
Actually you not need to create a custom ErrorListener since the default Volley's ErrorListener will be sufficient for your needs. 实际上,您不需要创建自定义的ErrorListener,因为默认的Volley的ErrorListener就可以满足您的需求。
Just pass an instance of the Response.ErrorListener to your LoginRequest and do your logic in the onErrorResponse() method. 只需将Response.ErrorListener的实例传递到LoginRequest并在onErrorResponse()方法中执行逻辑即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.