retrofit: First service call is slow

Having an issue where the first service call is very slow, then after the others are faster. my URL is HTTPS so I was wondering if it had something to do with that.

my Code:

OkHttpClient okClient = new OkHttpClient();
okClient.setReadTimeout(1, TimeUnit.MINUTES);
okClient.setConnectTimeout(1, TimeUnit.MINUTES);
Retrofit client = new Retrofit.Builder().baseUrl(baseURL).addConverterFactory(SimpleXmlConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(okClient).build();


dmvAPIInterface = client.create(DmvAPIInterface.class);
public interface DmvAPIInterface
    {
        @Headers({
                "Content-Type: application/x-www-form-urlencoded"
        })

        @GET("GetDataByLP")
        Call<DMVResponse> getResponse(@Query("LP") String LP,@Query("LPState") String LPState, @Query("jamaica") String Jamaica, @Query("demo") String Demo, @Query("deviceID") String DeviceID, @Query("OS") String OS);

I had to put the timeouts to 1 minute because if I didn’t, nothing happens and it just keeps loading for ever till I have to stop it. Usually the first call takes 15 - 20 seconds then after that the others are much faster (1-3 seconds)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 18 (1 by maintainers)

Most upvoted comments

OH COME ON : ITS BEEN 5 YEARS AND THIS BUG IS STILL HERE SOMEWHERE…

#metoo #still_facing_same_issue

This bug still exists and there is no clear solution for it, although for some reason I cannot reduce the timeout.

The problem is with mixed IPv4+IPv6 networks. It’s solved on Okhttp 5.0.0-alpha (https://square.github.io/okhttp/changelogs/changelog/#version-500-alpha4)

I got the key here: https://github.com/facebook/react-native/issues/28283#issuecomment-1416082231

dependencies {
    // others dependencies
  
    implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.11'
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:5.0.0-alpha.11'
}

In a nutshell, Retrofit/Android tries to connect first via IPv6. If there is a firewall issue as it was in my case, it keeps retrying until 60 seconds timeout have been reached. Only then it switches to IPv4.

See more here: https://github.com/square/okhttp/issues/6486

I recommend removing the AAAA dns entry to your server, until you have fixed the IPv6 issues. Good luck.

Am also facing similar issues where the first call is taking a long time. All the calls after that happens just in seconds.

First API taking approx the same time as specified in readTimeout. e.g. if it is 60 seconds then the first API will take 60 seconds. NOTE: this is not API issue, I changed the API, and still reproducing.