Retrofit: Failed to connect to api/ip address

Created on 8 Oct 2016  路  16Comments  路  Source: square/retrofit

Hi,

I am using Retrofit (v1.9) with a combination of OkHttp (v2.7.5). In production for a longer time I am getting this exception:

retrofit.RetrofitError: Failed to connect to api.domain.com/x.x.x.x:443
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at com.domain.android.api.$Proxy3.getFriends(Unknown Source)
Caused by java.net.ConnectException: Failed to connect to api.domain.com/x.x.x.x:443
at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.squareup.okhttp.Call.getResponse(Call.java:286)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:

It is affecting around 1-2% of the requests. So, is it something device dependent (which I think is not because users report they do not have issues with other apps), maybe it is some issue with server config or maybe OkHttp is using some proxy settings (should I use NO_PROXY?).

I have also tried to make use of Retrofit v1.9 and OkHttp v3.4.1 (with OkHttp 3 client implementation for Retrofit 1), but the situation is the same. This is why I assume there is an issue with OkHttp library.

Most helpful comment

Our users suffer the same issue. Use versions:

Retrofit: 2.1.0
okhttp: 3.4.1

The issue existed in older versions as well. 1-2% requests. All requests are through SSL. Most of them in battery save mode. Failed to reproduce. Builder looks like:

        return okhttp3.OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.MINUTES)
                .addNetworkInterceptor(headersInterceptor)
                .addNetworkInterceptor(tokenInterceptor)
                .addInterceptor(httpLoggingInterceptor)
                .build()

All 16 comments

Is it a problem with your network?

Which network? Server's or phone's?

Our users suffer the same issue. Use versions:

Retrofit: 2.1.0
okhttp: 3.4.1

The issue existed in older versions as well. 1-2% requests. All requests are through SSL. Most of them in battery save mode. Failed to reproduce. Builder looks like:

        return okhttp3.OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.MINUTES)
                .addNetworkInterceptor(headersInterceptor)
                .addNetworkInterceptor(tokenInterceptor)
                .addInterceptor(httpLoggingInterceptor)
                .build()

This is not a problem with Retrofit. As you can see by the stack trace, the HTTP client is asking the network to open a socket and that socket is failing for whatever reason. Networks are transient and flaky and it should be assumed that some percentage of connections will always fail.

In my case every time it happens, here is the stack trace

java.net.ConnectException: Failed to connect to myserver.com/139.162.1.242:7011
05-03 15:05:34.073 307-307/app.module W/System.err: at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:139)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:108)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:283)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.getResponse(Call.java:286)
05-03 15:05:34.075 307-307app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.qilotech.module.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:24)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.qilotech.module.api.AddCookiesInterceptor.intercept(AddCookiesInterceptor.java:31)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:204)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.access$100(Call.java:35)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:171)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
05-03 15:05:34.075 307-307/app.module W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
05-03 15:05:34.076 1903-2066/? D/AALService: Output backlight = 186/1023, CABC gain = 256
05-03 15:05:34.076 307-307/app.module W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
05-03 15:05:34.077 307-307/app.module W/System.err: at java.lang.Thread.run(Thread.java:818)

I face the same problem

Hi, Friends i am also facing same issue. is any one find the solution.

I'm facing this issue too

Hello Guys, There is very serious issue, I am facing too this issue. Please provide some steps for solving this issue

java.net.ConnectException: Failed to connect to myserver.com/192.162.1.242:3000
05-03 15:05:34.073 307-307/app.module W/System.err: at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:139)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:108)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
05-03 15:05:34.074 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:283)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.getResponse(Call.java:286)
05-03 15:05:34.075 307-307app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.qilotech.module.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:24)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.qilotech.module.api.AddCookiesInterceptor.intercept(AddCookiesInterceptor.java:31)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:204)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:232)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call.access$100(Call.java:35)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:171)
05-03 15:05:34.075 307-307/app.module W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
05-03 15:05:34.075 307-307/app.module W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
05-03 15:05:34.076 1903-2066/? D/AALService: Output backlight = 186/1023, CABC gain = 256
05-03 15:05:34.076 307-307/app.module W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
05-03 15:05:34.077 307-307/app.module W/System.err: at java.lang.Thread.run(Thread.java:818)

I'm facing the same issue. It seems three years already, not sure this is fixed or not?

Most of users are good, but some of them (5% i think) throw these errors:

  • Failed to connect to xx.com/xxxx:443
  • Unable to resolve host "xxx.com": No address associated with hostname

I'm facing the same issue. Any solution for this?

Paste following lines into manifest file
<application> <uses-library android:name="org.apache.http.legacy" android:required="false" /> </application>

I'm facing the same issue only in android studio emulators. but in real device and genymotion it working fine. Any solution for this?

I/okhttp.OkHttpClient: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to XXX.com/XXX:443

Paste following lines into manifest file
<application> <uses-library android:name="org.apache.http.legacy" android:required="false" /> </application>

your problem fixed?

Are you using an emulator for the testing or physical device. if emulator, please check your flight mode. put it off.

Are you using an emulator for the testing or physical device. if emulator, please check your flight mode. put it off.

a physical device, android 5

Was this page helpful?
0 / 5 - 0 ratings