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

callTimeout 不会回调到 onError #937

Open
chinalwb opened this issue Apr 28, 2020 · 0 comments
Open

callTimeout 不会回调到 onError #937

chinalwb opened this issue Apr 28, 2020 · 0 comments

Comments

@chinalwb
Copy link

设置 builder.callTimeout(1, TimeUnit.SECONDS); 之后不会执行到 AbsCallback 里面的 onError

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.callTimeout(1, TimeUnit.SECONDS);
        OkHttpClient okHttpClient = builder.build();

        OkGo.<T>post(url)
                .client(okHttpClient)
                ...
                .execute(AbsCallback);
protected void requestNetworkAsync() {
        rawCall.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                if (e instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) {
                    //retry when timeout
                    currentRetryCount++;
                    rawCall = request.getRawCall();
                    if (canceled) {
                        rawCall.cancel();
                    } else {
                        rawCall.enqueue(this);
                    }
                } else {
                    if (!call.isCanceled()) {  // 问题出在这里
                        Response<T> error = Response.error(false, call, null, e);
                        onError(error);
                    }
                }
            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                int responseCode = response.code();

                //network error
                if (responseCode == 404 || responseCode >= 500) {
                    Response<T> error = Response.error(false, call, response, HttpException.NET_ERROR());
                    onError(error);
                    return;
                }

                if (onAnalysisResponse(call, response)) return;

                try {
                    T body = request.getConverter().convertResponse(response);
                    //save cache when request is successful
                    saveCache(response.headers(), body);
                    Response<T> success = Response.success(false, body, call, response);
                    onSuccess(success);
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, call, response, throwable);
                    onError(error);
                }
            }
        });
    }

问题出在这里:
if (!call.isCanceled()) { // 问题出在这里

当 callTimeout 在 okHttp 里面被触发的时候,IOException 是: java.io.InterruptedIOException: timeout

并且 RealCall.this.isCanceled() 返回 true
所以就没有机会调用到指定的 onError 回调了。

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

1 participant