2

I've got this:

    final OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(someUrl)
            .build();

    Response response = client.newCall(request).execute();

I have looked at https://github.com/square/okhttp/wiki/Recipes#timeouts

I tried adding:

    .connectTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)

To the OkHttpClient builder, but I did not notice any difference even when I set the timeout number to 1 second.

Is there some way to listen for a timeout? What should I expect to happen if that timeout value is exceeded?

b85411
  • 9,420
  • 15
  • 65
  • 119

2 Answers2

1

If the request times out, it’ll fail with an IOException. This will happen either when you call execute() or when you read the response body.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
0

I also had this issue before. I solve the problem using the Asynchronous request's onFailure() callback. if Asynchronous suits you give it a try.

https://github.com/square/okhttp/wiki/Recipes#asynchronous-get https://github.com/square/okhttp/blob/okhttp_25/okhttp/src/main/java/com/squareup/okhttp/Callback.java#L20-#L26

amdev
  • 31
  • 1
  • 7