改进2:如何针对特定请求设置单独的超时时间? [英] Retrofit 2: How to set individual timeouts on specific requests?

查看:380
本文介绍了改进2:如何针对特定请求设置单独的超时时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过在Retrofit适配器中设置了全局超时

I have set a global timeout in my Retrofit adapter by doing

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(20, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(20, TimeUnit.SECONDS);

retrofit = new Retrofit.Builder()
.client(okHttpClient)
.build();

太好了!但我想为某些请求设置特定的超时时间例如

Great! But I would like to set an specific timeout for certain requests E.g.

public interface MyAPI {

    @GET()
    Call<Void> notImportant (@Url String url);

    @GET
    Call<Void> veryImportant(@Url String url);

所以 veryImportant 呼叫我想要35秒的超时时间,但是 notImportant 是默认的

So veryImportant calls I would like a timeout of 35 seconds but notImportant the default

这可能吗?

我的研究没有进展.

但是我遇到了这个问题,但不确定是否可以在翻新中使用

I came across this however but not sure if it will work in Retrofit

https://github.com/square/okhttp/wiki/配方#每次通话配置

感谢您的阅读.请帮忙.

Thank you for reading. Please help.

推荐答案

您可以通过创建改造对象工厂方法的重载方法来实现.可能看起来像这样.

You can do that by creating overloaded method of your retrofit object factory method. It's maybe look like this.

public class RestClient {

    public static final int DEFAULT_TIMEOUT = 20;

    public static <S> S createService(Class<S> serviceClass) {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        OkHttpClient client = httpClient.build();
        okHttpClient.setReadTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
            .client(client)
            .build();
        return retrofit.create(serviceClass);
    }

    public static <S> S createService(Class<S> serviceClass, int timeout) {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        OkHttpClient client = httpClient.build();
        okHttpClient.setReadTimeout(timeout, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(timeout, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(APIConfig.BASE_URL)
            .client(client)
            .build();
        return retrofit.create(serviceClass);
    }


}

如果您要使用默认的timout调用api,则可以这样调用它.

if you want to call api with default timout, you can call it look like this.

MyAPI api = RestClient.createService(MyAPI.class);
api.notImportant();

如果要通过身份验证调用api,请使用第二个:

And use the second one if you want to call api with authentication:

int timeout = 35;
MyAPI api2 = RestClient.createService(MYAPI.class, timeout);
api2.veryImportant();

另一种解决方案是通过使用不同的OkHttpClient配置创建不同的方法,而不是创建重载方法.希望此解决方案可以解决您的问题.

Another solution is by creating different method with different OkHttpClient configuration instead of creating overloaded method. Hope this solution fix your problem.

这篇关于改进2:如何针对特定请求设置单独的超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆