0

I am working on android application that call 'jersey' service by retrofit library. When my service take a long time, raised time out exception. I used below code to handle time out exception.

final OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(120, TimeUnit.SECONDS);
client.setReadTimeout(120, TimeUnit.SECONDS);
client.setWriteTimeout(120, TimeUnit.SECONDS);

restAdapter = new retrofit.RestAdapter.Builder()
        .setEndpoint(URL)
        .setErrorHandler(new MyErrorHandler())
        .setLogLevel(retrofit.RestAdapter.LogLevel.FULL)
        .setClient(new OkClient(client))
        .build();

But service calling failed after 30 seconds and my response body is null.

I think that time out settings does not work and retrofit did not wait until my service processing is complete while service is running in server and I log my response in server.

Application server: weblogic

Service library: jersey

Android jar files:okhttp-2.7.5, okhttp-urlconnection-2.7.5, retrofit-1.9.0

Please help me.

Thank you.

  • Possible duplicate of [Retrofit and OkHttpClient, catch connection timeout in failure method](https://stackoverflow.com/questions/29921667/retrofit-and-okhttpclient-catch-connection-timeout-in-failure-method) – jmarkmurphy Aug 14 '17 at 13:23

1 Answers1

0

use this

OkHttpClient httpClient = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request.Builder ongoing = chain.request().newBuilder();
                    ongoing.addHeader("Content-Type", "application/json; charset=utf-8");
                    return chain.proceed(ongoing.build());
                }
            })
            .connectTimeout(300, TimeUnit.SECONDS)
            .readTimeout(300, TimeUnit.SECONDS)
            .writeTimeout(300, TimeUnit.SECONDS)
            .build();

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.URL_API)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient)
            .build();
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
  • maybe server reject your request. – Rasoul Miri Jul 29 '17 at 10:10
  • after a 30 seconds, retrofit get a null response but response is created in server and weblogic throw this exception "java.io.IOException: An established connection was aborted by the software in your host machine" – Milad Alizadeh Jul 29 '17 at 12:09