欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

android 网络请求超时简单处理(基于rxjava)

程序员文章站 2022-03-24 21:55:59
implementation ‘com.squareup.retrofit2:adapter-rxjava:2.1.0’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_TI...

implementation ‘com.squareup.retrofit2:adapter-rxjava:2.1.0’

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 ResponeThrowable handleException(Throwable e) {
    ResponeThrowable ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
        switch (httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                //ex.code = httpException.code();
                ex.message = "网络错误";
                break;
        }
        return ex;
    } else if (e instanceof java.net.UnknownHostException) {
        ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
        ex.message = "网络错误";

        return ex;
    } else {
        ex = new ResponeThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误";

        return ex;
    }
}

class ERROR {
    /**
     * 未知错误
     */
    public static final int UNKNOWN = 1000;
    /**
     * 协议出错
     */
    public static final int HTTP_ERROR = 1003;

    /**
     * 网络错误
     */
    public static final int SSL_ERROR = 1005;
}

public static class ResponeThrowable extends Exception {
    public int code;
    public String message;

    public ResponeThrowable(Throwable throwable, int code) {
        super(throwable);
        this.code = code;
    }
}

}

//使用
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext() {
             
                    }

                    @Override
                    public void onError(Throwable e) {
                    if(e instanceof Exception){
                            //访问获得对应的Exception
                            Log.i("TAG",":code::"+ ExceptionHandle.handleException(e).code);
                            Log.i("TAG","message"+ ExceptionHandle.handleException(e).message);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

本文地址:https://blog.csdn.net/weixin_45189491/article/details/108977310

相关标签: