gpt4 book ai didi

java - 如何使用 retrofit2 动态设置超时?

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:25 27 4
gpt4 key购买 nike

public class Router {

...

private static Retrofit retrofit = null;

...

public Retrofit getRetrofit() {
if (retrofit == null) {

...

OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();

retrofit = new Retrofit.Builder()
.baseUrl("***")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
}
return retrofit;
}

...

}

我在 OkHttpClient 中设置超时。现在有一个 API 需要不同的超时。

我该怎么办?

有这样的东西吗?

@Timeout
Observable<ResponseBody> exapmle(@Timeout("connect") int connect
, @Timeout("read") int read
, @Timeout("write") int write);

最佳答案

enter image description here

非常感谢。

这是我的代码:

public static final String CONNECT_TIMEOUT = "CONNECT_TIMEOUT";
public static final String READ_TIMEOUT = "READ_TIMEOUT";
public static final String WRITE_TIMEOUT = "WRITE_TIMEOUT";

...

Interceptor timeoutInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();

int connectTimeout = chain.connectTimeoutMillis();
int readTimeout = chain.readTimeoutMillis();
int writeTimeout = chain.writeTimeoutMillis();

String connectNew = request.header(CONNECT_TIMEOUT);
String readNew = request.header(READ_TIMEOUT);
String writeNew = request.header(WRITE_TIMEOUT);

if (!TextUtils.isEmpty(connectNew)) {
connectTimeout = Integer.valueOf(connectNew);
}
if (!TextUtils.isEmpty(readNew)) {
readTimeout = Integer.valueOf(readNew);
}
if (!TextUtils.isEmpty(writeNew)) {
writeTimeout = Integer.valueOf(writeNew);
}

return chain
.withConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
.withReadTimeout(readTimeout, TimeUnit.MILLISECONDS)
.withWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS)
.proceed(request);
}
};

OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS)
.addInterceptor(timeoutInterceptor)
.build();

retrofit = new Retrofit.Builder()
.baseUrl(Host.Develop.ACCOUNT_HOST)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(httpClient)
.build();

...

示例用法:

/**
* check account status
*
* @param account
* @return
*/
@Headers({"CONNECT_TIMEOUT:10000", "READ_TIMEOUT:10000", "WRITE_TIMEOUT:10000"})
@GET("login/prelogin")
Observable<ResponseBody> preLogin(@Query("account") String account);

/**
* check account status
*
* @param connectTimeout
* @param readTimeout
* @param writeTimeout
* @param account
* @return
*/
@GET("login/prelogin")
Observable<ResponseBody> preLogin(
@Header("CONNECT_TIMEOUT") String connectTimeout,
@Header("READ_TIMEOUT") String readTimeout,
@Header("WRITE_TIMEOUT") String writeTimeout,
@Query("account") String account);

关于java - 如何使用 retrofit2 动态设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47347318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com