6

I'm using Retrofit and OkHttp to connect to server. in most times it works good. but is some case it fail. some times HTTP FAILED: java.net.UnknownHostException and some times java.net.SocketTimeoutException. this error is just when I'm using home wifi to connect to server. but when I use mobile network it is good and there is no problem. using with emulator is good. it happens just with real device. and just some times not all times.

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build();



    retrofit = new Retrofit.Builder()
            .baseUrl(ClientConfigs.REST_API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();


compile 'com.squareup.retrofit2:retrofit:2.2.0'
 compile 'com.squareup.retrofit2:converter-gson:2.1.0'
2
  • Stack Overflow is for programming questions. What is your question? If your question is "how do I avoid these errors?", the answer is "stop using the Internet". You will run into these sorts of problems a lot when you release your app and it gets used on all sorts of networks in all sorts of situations. Commented May 12, 2017 at 15:02
  • I am also facing similar problem, happening in very very few devices that too intermittently, we have are already tried basic troubleshooting including app reinstallation, User has good internet connectivity, other apps are in same device are not facing connectivity issues, The major exception we get in affected devices are "java.net.SocketTimeoutException: failed to connect to my.domain.com/64:ff9b::dea:c77e (port 443) from /2409:4089:bc92:b09:af24:6312:e6da:ca00 (port 44860) after 20000ms: isConnected failed: ETIMEDOUT (Connection timed out)" OR "java.net.UnknownHostException..." Commented Mar 5, 2023 at 8:22

2 Answers 2

1

My guess your home wify is not stable as it seems.

OkHttpClient throws UnknownHostException when specified host cant be resolved (this happens when you have no internet for example).

For the SocketTimeout, OkHttp will throw this when request timeout is reached (For example if your servers takes more than 30 seconds to send back the result, but this also can be caused by your internet connection to the server)

Sign up to request clarification or add additional context in comments.

2 Comments

i have the same issue but no luck on fixing it. Already added the timeout but didn't help. Any suggestion?
This answer was provided 2 years ago, and tbh its not a real solution to this issue.
0

In my current project I have the same issue. Probably it is problem with your network hardware/software. I have two routers. With first router my app works perfectly. Second router is located in another room. When I try to use app nearly it works good. But when I go to another room and try to make request with good signal - I receive java.net.SocketTimeoutException after 5 seconds (my timeout in Retrofit is 120 seconds).

1 Comment

Agree with you. I also have this problem on one device with Wi-Fi, but only after upgrade of an application. Turning to mobile network fixes the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.