【问题标题】:Retrofit 2: How to set individual timeouts on specific requests?改造 2:如何为特定请求设置单独的超时?
【发布时间】:2017-04-25 14:40:11
【问题描述】:

我在我的改造适配器中设置了一个全局超时

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

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

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

public interface MyAPI {

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

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

所以veryImportant 调用我想要 35 秒的超时,但 notImportant 是默认值

这可能吗?

我的研究一落千丈。

我遇到了这个,但不确定它是否可以在 Retrofit 中工作

https://github.com/square/okhttp/wiki/Recipes#per-call-configuration

感谢您的阅读。请帮忙。

【问题讨论】:

    标签: android retrofit okhttp retrofit2 okhttp3


    【解决方案1】:

    您可以通过创建改造对象工厂方法的重载方法来做到这一点。大概是这个样子。

    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);
        }
    
    
    }
    

    如果你想用默认的timeout调用api,你可以这样称呼它。

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

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

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

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

    【讨论】:

    【解决方案2】:

    请检查一下。

    如果您使用的是compile 'com.squareup.retrofit:retrofit:1.9.0',请使用下面给出的同一个 squareup 库中的 okhttp

    compile 'com.squareup.okhttp:okhttp:2.7.2'
    

    这里有我的示例代码。

                final OkHttpClient okHttpClient = new OkHttpClient();
                okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
                okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    
                RestAdapter restAdapter = new RestAdapter.Builder()
                        .setEndpoint(API)
                        .setLogLevel(RestAdapter.LogLevel.FULL)
                        .setClient(new OkClient(okHttpClient))
                        .build();
    

    注意:60 - 改造将等到 60 秒显示超时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2020-03-24
      • 2017-06-28
      • 2019-07-25
      相关资源
      最近更新 更多