4

I am Using Retrofit 2. The Issue I am facing is: The first response is coming late after eating up all the timeperiod mentioned in connectTimeout. And after the first call all the other webservice call runs taking no time.

Also, If I run it on the Wifi network it works without any issue, i.e The First Webservice runs and give response without giving any issue. But if I run it on Mobile/Device net(Internet provided by Service provider on the Mobile device) the response is again delivered after eating up all the time mentioned in the connectTimeout.

Below is my Retrofit API Class Code:-

public class APIClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request originalRequest = chain.request();

                    Request request = originalRequest.newBuilder()
                            .header(Constants.KeyConstants.HEADER_AUTH, Constants.ConstantKeyTag.HEADER_AUTH_VALUE)
                            .header(Constants.KeyConstants.HEADER_CONTENT_TYPE, Constants.ValuesConstant.HEADER_CONTENT_TYPE_VALUE) 
                            .method(originalRequest.method(), originalRequest.body())
                            .build();

                    return chain.proceed(request);
                }
            });


            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();

            retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.AppSpecificConstants.WS_BASEURL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }

    final static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();

}

I have refer the below links but has found nothing helpful:

https://github.com/square/retrofit/issues/1489

https://stackoverflow.com/questions/39915469/retrofit-2-delayed-response

https://stackoverflow.com/questions/46034047/the-response-from-the-retrofit-call-is-too-slow-and-it-returns-a-null-list?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Can anyone put the enlightment on the issue and any solution to resolve it.

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50

0 Answers0