【问题标题】:Set connectionTimeOut and SocketTimeout in retrofit在改造中设置 connectionTimeOut 和 SocketTimeout
【发布时间】:2016-06-12 08:48:41
【问题描述】:

我正在编写以下代码以在httpClient 上设置timeout。但是,我的代码指出未找到 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);
    }


}

【问题讨论】:

  • 它已经在下面的链接中被 Asnwerd Duplicate

标签: android okhttp retrofit2


【解决方案1】:

自己找到了解决方案。 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 是如何创建的。

【讨论】:

    【解决方案2】:

    添加库依赖

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

    编译'com.squareup.okhttp:okhttp:x.x.x'

    也许此链接可以帮助您: How to set timeout in Retrofit library?

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 2011-11-13
      • 1970-01-01
      • 2013-09-18
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 2017-05-04
      相关资源
      最近更新 更多