public class exceptionhandle {
private static final int unauthorized = 401;
private static final int forbidden = 403;
private static final int not_found = 404;
private static final int request_timeout = 408;
private static final int internal_server_error = 500;
private static final int bad_gateway = 502;
private static final int service_unavailable = 503;
private static final int gateway_timeout = 504;
public static responseexception handleexception(throwable e){
//转换成responseexception,根据状态码判定错误信息
responseexception ex;
if(e instanceof httpexception){
httpexception httpexception=(httpexception)e;
/**
* 传入状态码,根据状态码判定错误信息
*/
ex=new responseexception(e,error.http_error);
switch (httpexception.code()){
case unauthorized:
ex.message="未验证";
break;
case forbidden:
ex.message="服务禁止访问";
break;
case not_found:
ex.message="服务不存在";
break;
case request_timeout:
ex.message="请求超时";
break;
case gateway_timeout:
ex.message="网关超时";
break;
case internal_server_error:
ex.message="服务器内部错误";
break;
case bad_gateway:
break;
case service_unavailable:
break;
default:
ex.message = "网络错误";
break;
}
return ex;
}else if(e instanceof jsonparseexception
|| e instanceof jsonexception
|| e instanceof parseexception){
ex=new responseexception(e,error.parse_error);
ex.message="解析错误";
return ex;
}else if(e instanceof connectexception){
ex=new responseexception(e,error.netword_error);
ex.message="连接失败";
return ex;
}else if(e instanceof javax.net.ssl.sslhandshakeexception){
ex=new responseexception(e,error.ssl_error);
ex.message="证书验证失败";
return ex;
}else {
ex=new responseexception(e,error.unknown);
ex.message="未知错误";
return ex;
}
}
/**
* 约定异常
*/
public static class error{
/**
* 自定义异常
*/
private static final int unauthorized = 401;//请求用户进行身份验证
private static final int unrequest=403;//服务器理解请求客户端的请求,但是拒绝执行此请求
private static final int unfindsource=404;//服务器无法根据客户端的请求找到资源
private static final int severerror=500;//服务器内部错误,无法完成请求。
/**
* 协议出错
*/
public static final int http_error = 1003;
/**
* 未知错误
*/
public static final int unknown = 1000;
/**
* 解析错误
*/
public static final int parse_error = 1001;
/**
* 网络错误
*/
public static final int netword_error = 1002;
/**
* 证书出错
*/
public static final int ssl_error = 1005;
}
/**
* 自定义throwable
*/
public static class responsethrowable extends exception{
public int code;
public string message;
public responsethrowable(throwable throwable,int code){
super(throwable);
this.code=code;
}
}
/**
* 服务器异常
*/
public class serverexception extends runtimeexception{
public int code;
public string message;
}
/**
* 统一异常类,便于处理
*/
public static class responseexception extends exception{
public int code;
public string message;
public responseexception (throwable throwable,int code){
super(throwable);
this.code=code;
}
}
}
然后自己定义了一个observer
public abstract class baseobserver<t> implements observer<t> {
private context context;
public baseobserver(context context){
this.context=context;
}
@override
public void onsubscribe(disposable d) {
}
@override
public void onnext(t t) {
}
@override
public void onerror(throwable e) {
if(e instanceof exceptionhandle.responseexception){
onerror((exceptionhandle.responseexception)e);
}else{
onerror(new exceptionhandle.responseexception(e,exceptionhandle.error.unknown));
}
}
@override
public void oncomplete() {
}
public abstract void onerror(exceptionhandle.responseexception exception);
}