Okhttp: OkHttpClient ReadTimeout affected by response.body() ?

Created on 5 Nov 2014  路  2Comments  路  Source: square/okhttp

After setting some timeout values my GET request still surpasses 32 seconds. I noticed that the main bottleneck is on response.body() method which I don't know if has something to do with the ReadTimeout value.

Could it be that the json that I麓m retrieving its just big and substracting it from the Response object it麓s slow?

APIClient  D  getConnectTimeout: 10000
                 D  getWriteTimeout: 10000
                 D  getReadTimeout: 20000

Thanks.

Most helpful comment

Thanks @swankjesse. It worked like a charm.

Response response = client.newCall(request).execute();
BufferedSource bufferedSource = response.body().source();
Timeout timeout = bufferedSource.timeout();
timeout.deadline(20, TimeUnit.SECONDS);
String str = response.body().string();

Throwed the following.

System.err  W  java.io.InterruptedIOException: timeout

I麓m adding the code here just in case someone needs it.

All 2 comments

The read timeout will only trigger if no data is returned for the full timeout. If the response is being streamed steadily but slowly, the timeout won't trigger. You might be able to get that with Okio's deadline.

You can get the timeout from response.body().source().
http://square.github.io/okio/okio/Timeout.html

Thanks @swankjesse. It worked like a charm.

Response response = client.newCall(request).execute();
BufferedSource bufferedSource = response.body().source();
Timeout timeout = bufferedSource.timeout();
timeout.deadline(20, TimeUnit.SECONDS);
String str = response.body().string();

Throwed the following.

System.err  W  java.io.InterruptedIOException: timeout

I麓m adding the code here just in case someone needs it.

Was this page helpful?
0 / 5 - 0 ratings