在改造中设置connectionTimeOut和SocketTimeout

时间:2016-02-29 10:38:27

标签: android okhttp retrofit2

我正在使用以下代码在timeout上设置httpClient。但是,我的代码指出未找到setConnectTimeoutsetReadTimeout。我做错了什么?

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);
    }


}

2 个答案:

答案 0 :(得分:0)

添加库依赖项

在build.gradle中,包含以下行:

compile&#39; com.squareup.okhttp:okhttp:x.x.x&#39;

也许这个链接可以为您提供帮助: How to set timeout in Retrofit library?

答案 1 :(得分:0)

自己找到解决方案。 ServiceGeneratorFileUpload班级

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);
    }
}

现在使用上面的代码,我可以使用...

创建我的api服务
MyApiService myapiservice = ServiceGeneratorFileUpload.createService(MyApiService.class)

使用有关改造的文档来了解如何创建MyApiService。