Issue
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'
Solution
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).
Answered By - kostyabakay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.