1

As the title says, I'm encountering this error when trying to access a distant database. This is a part of my crash log :

retrofit.RetrofitError: failed to connect to xx.xx.xx.xx/xx.xx.xx.x (port 80) from /xx.xx.xx.xx (port 52908) after 15000ms

Caused by: java.net.SocketTimeoutException: failed to connect to xx.xx.xx.xx/xx.xx.xx.x (port 80) from /xx.xx.xx.xx (port 52908) after 15000ms

The app works fine since I tested it with various devices and the server is up and running.

a workaround would be to increase the timeout but is there any other way to handle this issue? All posts I read mentioning this, only suggest to set a high timeout. Or should I just set a use case where this issue occurs and alert the user to check his connection ?

Any advice is much appreciated

Note :

  • retrofit version I'm using is : 1.9.0
  • OkHttp3 version is 3.0.1
user 007
  • 821
  • 10
  • 31

1 Answers1

1

Most likely you get this exception because you are trying to read data from the request but it exceeds the default timeout value

This is either a network connectivity issue or your backend api is taking too long to respond for some reason. So there is no way to fix this from client side other than increasing the default timeout

This could be a

  • Connection Timeout
  • Read Timeout
  • Write Timeout

identify the type of timeout happening in your case and attach the client to retrofit

Show Exception Message

@Override
public void onFailure(Call<ResponseType> call, Throwable t) {
    if(t instanceof SocketTimeoutException){
       String message = "Socket Time out!!";
    }
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
  • Thanks for the informations. Is it good practice to create a use case checking if the timeout is exceeded then the user will be shown a message to check his connection or try later instead of leaving him in the black ?? and by how much should I increase my timeout ? – user 007 Jun 20 '18 at 10:50
  • 1
    *Is it good practice to create a use case checking* - yes, It would be a good choice to show a message if the timeout exceeds the default value(you could check this in retrofit's failure callback if the exception is due to `SocketTimeoutException`, see edited answer ). *how much should I increase my timeout* - this depends on your case. – Navneet Krishna Jun 20 '18 at 11:01
  • a quick question ! since I couldn't reproduce the error with my devices does handling the error on the onFailure method like you did (showing a message) prevent the app from crashing ? or I should wrap my method where I call a retrofit connection with w try/catch ? – user 007 Jun 22 '18 at 05:37
  • 1
    If that is the case you will have to intercept the timeout even before entering the callbacks.You were right about the `try/catch`, but it should be done inside the interceptor method. This can be done by attaching an interceptor to your `OkHttpClient` like [this](https://stackoverflow.com/a/34091088/8009433) – Navneet Krishna Jun 22 '18 at 06:56
  • thanks for the tip and link. but in my case since I'm using `Retrofit 1.9` I ended up using `try/catch` to catch `RetrofitError` locally (not on all my requests only the one causing the crash) – user 007 Jun 22 '18 at 12:02