如何在Android的Retrofit库中设置连接超时?

时间:2017-02-19 07:57:33

标签: android retrofit retrofit2

我已经为我的Android应用程序使用了Retrofit库。我需要将连接超时设置为120秒。我能怎么做 ?

版本:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

OperatingApiClient:

    public class OperatingApiClient {
    public static final String BASE_URL = "http://172.16.2.39/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
    }

TargetFileApiInterface:

public interface TargetFileApiInterface {
@GET("NNGOperating/GetTargetFileList")
Call<TargetFileApiResponse> getTargetFileList(@Query("api_key") 
String apiKey);

}

TargetFileApiResponse:

    public class TargetFileApiResponse {
    @SerializedName("TargetFileList")
    private List<TargetFile> targetfileslist;

    public TargetFileApiResponse(List<TargetFile> targetfileslist) {
        this.targetfileslist = targetfileslist;
    }

    public List<TargetFile> getTargetfileslist() {
        return targetfileslist;
    }

    public void setTargetfileslist(List<TargetFile> targetfileslist) {
        this.targetfileslist = targetfileslist;
    }
    }

3 个答案:

答案 0 :(得分:6)

您可以为OkHttp

设置配置
OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(yourTime, TimeUnit.SECONDS)
        .writeTimeout(yourTime, TimeUnit.SECONDS)
        .readTimeout(yourTime, TimeUnit.SECONDS)
        .build();

之后,设置客户端进行改造

retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

答案 1 :(得分:3)

它正在发挥作用。

public class OperatingApiClient {
    public static final String BASE_URL = "http://172.16.2.39/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(120, TimeUnit.SECONDS)
            .connectTimeout(120, TimeUnit.SECONDS)
            .build();

}

答案 2 :(得分:2)

尝试以下操作,为改装构建器设置httpClient。

OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS); // I am using 60 seconds you can user your own


 public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())  //setting your custom client
                    .build();
        }
        return retrofit;
    }