Connection never timeout at OkHttp

2019-08-18 06:01发布

With okhttp, I define okhttp connection timeout value to 3 secs to get timeout error.

OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
            .connectTimeout(3, TimeUnit.SECONDS)
            .readTimeout(3, TimeUnit.SECONDS)
            .writeTimeout(3, TimeUnit.SECONDS);

I used retrofit and rxjava to do network call and handle network error case with onError() consumer at subscribe method.

mTheApi.getMenuAPI(request)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    response -> {
                        Log.d(TAG, "getMenuAPI: " + response);
                    },
                    throwable -> {
                        // network error
                        Log.e(TAG, "getMenuAPI error: " + throwable.getMessage());
                    }
            ));

But when I call API with these timeout settings, connection never timeout for long API response time.

Normally It have to time out after 3 secs but still keep continue calling API more than 3 secs (more or less 10 secs).

How should I handle connection timeout for long response time?

Thanks in advance.

0条回答
登录 后发表回答