Volley
网络请求错误返回
VolleyError
package com.android.volley;
/** Exception style class encapsulating Volley errors */
@SuppressWarnings("serial")
public class VolleyError extends Exception {
public final NetworkResponse networkResponse;
private long networkTimeMs;
public VolleyError() {
networkResponse = null;
}
public VolleyError(NetworkResponse response) {
networkResponse = response;
}
public VolleyError(String exceptionMessage) {
super(exceptionMessage);
networkResponse = null;
}
public VolleyError(String exceptionMessage, Throwable reason) {
super(exceptionMessage, reason);
networkResponse = null;
}
public VolleyError(Throwable cause) {
super(cause);
networkResponse = null;
}
/* package */ void setNetworkTimeMs(long networkTimeMs) {
this.networkTimeMs = networkTimeMs;
}
public long getNetworkTimeMs() {
return networkTimeMs;
}
}
可以看到Volley这个类,写了5个平时可能用到的错误返回的构造方法。源码很清除,具体就不解释了。但是我搜索这个类被引用的地方的时候,发现很多类都引用了这个类,volley就47个java文件,有这么多类竟然引用到VolleyError,说明Volley设计的非常的简洁,而且严谨,一个不多一个不少,都充分的去利用每个类的价值,应该是极高的构架师来构架的volley这个项目。(试想平时我们自己写的一个接口或者抽象类,写的有这样的简单吗?而且被很多的类(不同的类型的类)引用)。
服务器错误类 ServerError,ServerError.class继承自VolleyError.class
package com.android.volley;
/** Indicates that the server responded with an error response. */
@SuppressWarnings("serial")
public class ServerError extends VolleyError {
public ServerError(NetworkResponse networkResponse) {
super(networkResponse);
}
public ServerError() {
super();
}
}
解析错误类 ParseError
package com.android.volley;
/** Indicates that the server's response could not be parsed. */
@SuppressWarnings("serial")
public class ParseError extends VolleyError {
public ParseError() {}
public ParseError(NetworkResponse networkResponse) {
super(networkResponse);
}
public ParseError(Throwable cause) {
super(cause);
}
}
网络错误类 NetworkError
package com.android.volley;
/** Indicates that there was a network error when performing a Volley request. */
@SuppressWarnings("serial")
public class NetworkError extends VolleyError {
public NetworkError() {
super();
}
public NetworkError(Throwable cause) {
super(cause);
}
public NetworkError(NetworkResponse networkResponse) {
super(networkResponse);
}
}
AuthFailureError 认证失败类
package com.android.volley;
import android.content.Intent;
/** Error indicating that there was an authentication failure when performing a Request. */
@SuppressWarnings("serial")
public class AuthFailureError extends VolleyError {
/** An intent that can be used to resolve this exception. (Brings up the password dialog.) */
private Intent mResolutionIntent;
public AuthFailureError() {}
public AuthFailureError(Intent intent) {
mResolutionIntent = intent;
}
public AuthFailureError(NetworkResponse response) {
super(response);
}
public AuthFailureError(String message) {
super(message);
}
public AuthFailureError(String message, Exception reason) {
super(message, reason);
}
public Intent getResolutionIntent() {
return mResolutionIntent;
}
@Override
public String getMessage() {
if (mResolutionIntent != null) {
return "User needs to (re)enter credentials.";
}
return super.getMessage();
}
}
时间错误类
package com.android.volley;
/** Indicates that the connection or the socket timed out. */
@SuppressWarnings("serial")
public class TimeoutError extends VolleyError {}
….还有一个子类没有列出
我在想为什么VolleyError 和子类没有用接口了,而是用类来表示
我看了下子类被用到的地方,发现基本上都是可以new Object()即用到了实例,这个平时开发中需要去学习的。
对于VolleyError:
Java中接口不能被实例化的,可以继承接口(不能继承类)。
显示不满足VolleyError需求。