Manish Jangid
Manish Jangid

Reputation: 185

Set connectionTimeOut and SocketTimeout in retrofit

I am working on the following code to set the timeout on httpClient. However, my code points out that setConnectTimeout and setReadTimeout not found. What am I doing wrong?

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import in.futuretrucks.Constant.Constant;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;

import static java.util.concurrent.TimeUnit.*;

public class ServiceGeneratorFileUpload {

    public static final String API_BASE_URL = Constant.BASE_URL;

    private static OkHttpClient httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
    httpClient.setReadTimeout(15, TimeUnit.SECONDS);

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {

        httpClient.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();

                Request.Builder requestBuilder = original.newBuilder()
                        .header("cache-control", "no-cache")
                        .header("Content-Type", "multipart/form-data")
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });

        //set logging interceptor. Disabled as of now. Useful to see request and response feature
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.interceptors().add(interceptor);

        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }


}

Upvotes: 0

Views: 672

Answers (2)

Manish Jangid
Manish Jangid

Reputation: 185

Found the solution myself. ServiceGeneratorFileUpload Class

public class ServiceGeneratorFileUpload {

public static final String API_BASE_URL = Constant.BASE_URL;

private static OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(4, TimeUnit.MINUTES);
    client.setReadTimeout(4, TimeUnit.MINUTES);
    return client;
}

private final static OkHttpClient httpClient = getClient();

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
    }
}

Now with the above code, I am able to create my api service using ...

MyApiService myapiservice = ServiceGeneratorFileUpload.createService(MyApiService.class)

Use documentation on retrofit to understand how MyApiService is created.

Upvotes: 1

Anton Bevza
Anton Bevza

Reputation: 463

Add the library dependency

In the build.gradle, include this line:

compile 'com.squareup.okhttp:okhttp:x.x.x'

Maybe this link help can you: How to set timeout in Retrofit library?

Upvotes: 0

Related Questions