Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如果未登录,怎么跳转去登录. #38

Open
nideWind opened this issue Feb 12, 2019 · 7 comments
Open

请问如果未登录,怎么跳转去登录. #38

nideWind opened this issue Feb 12, 2019 · 7 comments

Comments

@nideWind
Copy link

public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(e, ApiCode.Request.HTTP_ERROR);
switch (httpException.code()) {
case ApiCode.Http.UNAUTHORIZED:
case ApiCode.Http.FORBIDDEN:
case ApiCode.Http.NOT_FOUND:
case ApiCode.Http.REQUEST_TIMEOUT:
case ApiCode.Http.GATEWAY_TIMEOUT:
case ApiCode.Http.INTERNAL_SERVER_ERROR:
case ApiCode.Http.BAD_GATEWAY:
case ApiCode.Http.SERVICE_UNAVAILABLE:
default:
ex.message = "NETWORK_ERROR";
break;
}
return ex;
}

这段代码真的不是在作死吗?? 限制了对网络错误的处理. 作为框架应该是去开放功能,而不是去乱限制前端的UI处理吧. 真实人才啊.

@GreenVegetables
Copy link

这是http基础code,哪里限制你处理错误了,自己错误通过拦截器自己处理,作者开放框架本来好事一件,即使有瑕疵,麻烦语气好点。你才是个人才,自己去封装retrofit吧,没人限制你

@nideWind
Copy link
Author

语气有点不太好,不好意思.

但是作为框架来说,确实是限制了开发者对response的处理.
1.第一步,拦截响应,转换数据.

2.分析数据,拦截异常.

3.问题出现, 此处恶意,瞎封装, 阻碍了开发者的正常异常处理.

建议:网络请求并非是去获取 后台的数据状态码而进行处理的. 请在http的响应状态码处去获取在处理, 不要干涉json数据里面的任何状态内容。

@nideWind
Copy link
Author

1.第一步,拦截响应,转换数据.
protected ObservableTransformer<ResponseBody, T> norTransformer(final Type type) {
return new ObservableTransformer<ResponseBody, T>() {
@OverRide
public ObservableSource apply(Observable apiResultObservable) {
return apiResultObservable
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.map(new ApiFunc(type))
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(new ApiRetryFunc(retryCount, retryDelayMillis));
}
};
}

2.分析数据,拦截异常.
@OverRide
public Observable apply(Observable observable) throws Exception { return observable .flatMap(new Function>() {
@OverRide
public ObservableSource<?> apply(Throwable throwable) throws Exception {
if (++retryCount <= maxRetries && (throwable instanceof SocketTimeoutException
|| throwable instanceof ConnectException)) {
ViseLog.d("get response data error, it will try after " + retryDelayMillis
+ " millisecond, retry count " + retryCount);
return Observable.timer(retryDelayMillis, TimeUnit.MILLISECONDS);
}
return Observable.error(ApiException.handleException(throwable));
}
});
}
3.问题出现, 此处恶意,瞎封装, 阻碍了开发者的正常异常处理.

public static ApiException handleException(Throwable e) {
    ApiException ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ApiException(e, ApiCode.Request.HTTP_ERROR);
        switch (httpException.code()) {
            case ApiCode.Http.UNAUTHORIZED:
            case ApiCode.Http.FORBIDDEN:
            case ApiCode.Http.NOT_FOUND:
            case ApiCode.Http.REQUEST_TIMEOUT:
            case ApiCode.Http.GATEWAY_TIMEOUT:
            case ApiCode.Http.INTERNAL_SERVER_ERROR:
            case ApiCode.Http.BAD_GATEWAY:
            case ApiCode.Http.SERVICE_UNAVAILABLE:
            default:
                ex.message = "NETWORK_ERROR";
                break;
        }
        return ex;
    } else if (

這個问题确实存在, 你要犟, 我比你更犟. 我不告诉你这个问题,估计你永远也发现不了.

@GreenVegetables
Copy link

不好意思,我不是作者,此框架本人也在使用,并无什么影响非常大的问题。至于你说的,上面已经说了这是http基础code,就是响应码,压根没干涉你的json,你别给作者建议了,还是好好读读源码吧

@GreenVegetables
Copy link

你标题的问题,看看netexpand,会有帮助,对着范例自己可以实现

@mulinAi
Copy link

mulinAi commented Feb 21, 2019

@nideWind 可以参考下我刚刚发的issue,是否可以解决你的问题 #39

@shangjingxie
Copy link

ViseHttp.POST("login")
.baseUrl("http://121.41.44.177/")
.addParam("mobile", "152502016")
.addParam("password", "wrewefa")
.request(new ACallback() {
@OverRide
public void onFail(int errCode, String errMsg) {
ViseLog.e("request errorCode:" + errCode + ",errorMsg:" + errMsg);
}

            @Override
            public void onSuccess(String data) {
                ViseLog.i("request onSuccess!" + data);
                mShow_response_data.setText(data);
            }
        });

应用场景: 当服务器返回 类似 { “code”:400, "message:"用户不存在"} 这样的返回数据,调用demo的 ViseHttp.POST, 返回的exception 为: code :1000, msg: UNKNOWN。 这明显与我们预期的返回:“用户不存在” 提示有冲突。请问大佬这应该怎么去改呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants