Reputation: 177
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'
Upvotes: 6
Views: 4636
Reputation: 2068
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)
Upvotes: 1
Reputation: 1689
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).
Upvotes: 0