在改造中设置自定义连接超时不起作用

时间:2019-06-26 05:57:00

标签: android retrofit2 okhttp

改装不采用自定义超时,默认情况下,连接超时需要10秒。

我已经尝试了所有设置自定义超时的方法,如下面的代码。

//service generator class
    public class ServiceGenerator{

    private Context context;

    private static OkHttpClient.Builder httpClient =new OkHttpClient.Builder();

     private static Retrofit.Builder builder =
                    new Retrofit.Builder()
                            .baseUrl(UriList.BASE_URL)
                            .client(getRequestHeader())
                          .addConverterFactory(GsonConverterFactory.create());

    public static HttpLoggingInterceptor setLogger() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        return logging;
    }


    public static <S> S createService(Class<S> serviceClass, Context context) {

        if (!httpClient.interceptors().isEmpty()) {

            httpClient.interceptors().clear();
        }


            httpClient.addInterceptor(new NetworkConnectionInterceptor(context));
            httpClient.addInterceptor(setLogger());
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    Request.Builder requestBuilder = original.newBuilder()
    MyApplication.getInstance().getApiCredential())
                            .header("LocalSystemDate", MyApplication.getInstance().getCurrentDateAndTimeWithTimeZone())
                            .method(original.method(), original.body());
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            });

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


    private static OkHttpClient getRequestHeader() {

        return new OkHttpClient.Builder()
                .readTimeout(2, TimeUnit.MINUTES)
                .connectTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                .build();
    }


}

10秒后,我收到错误消息:

D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException

1 个答案:

答案 0 :(得分:0)

创建改造对象时您使用了错误的httpclient

您必须在createService(Class<S> serviceClass, Context context)内添加以下代码行

 httpClient.readTimeout(2, TimeUnit.MINUTES)
           .connectTimeout(2, TimeUnit.MINUTES)
           .writeTimeout(2, TimeUnit.MINUTES);

因此您的createService(Class<S> serviceClass, Context context)将是

public static <S> S createService(Class<S> serviceClass, Context context) {

            if (!httpClient.interceptors().isEmpty()) {

                httpClient.interceptors().clear();
            }


            httpClient.addInterceptor(new NetworkConnectionInterceptor(context));
            httpClient.addInterceptor(setLogger());
            httpClient.readTimeout(2, TimeUnit.MINUTES)
                    .connectTimeout(2, TimeUnit.MINUTES)
                    .writeTimeout(2, TimeUnit.MINUTES);
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    Request.Builder requestBuilder = original.newBuilder()
                    MyApplication.getInstance().getApiCredential())
                            .header("LocalSystemDate", MyApplication.getInstance().getCurrentDateAndTimeWithTimeZone())
                            .method(original.method(), original.body());
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            });

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

现在可以使用。试试吧。