如何在Retrofit库中设置超时? [英] How to set timeout in Retrofit library?

查看:182
本文介绍了如何在Retrofit库中设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用 Retrofit 库,我想将超时设置为60秒。 Retrofit有办法做到这一点吗?

I am using Retrofit library in my app, and I'd like to set a timeout of 60 seconds. Does Retrofit have some way to do this?

我以这种方式设置Retrofit:

I set Retrofit this way:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setServer(BuildConfig.BASE_URL)
    .setConverter(new GsonConverter(gson))
    .build();

如何设置超时?

推荐答案

您可以在底层HTTP客户端上设置超时。如果您未指定客户端,则Retrofit将创建一个具有默认连接和读取超时的客户端。要设置自己的超时,您需要配置自己的客户端并将其提供给 RestAdapter.Builder

You can set timeouts on the underlying HTTP client. If you don't specify a client, Retrofit will create one with default connect and read timeouts. To set your own timeouts, you need to configure your own client and supply it to the RestAdapter.Builder.

一个选项是使用 OkHttp 客户端,也来自Square。

An option is to use the OkHttp client, also from Square.

1。添加库依赖项

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

In the build.gradle, include this line:

compile 'com.squareup.okhttp:okhttp:x.x.x'

其中 xxx 是所需的库版本。

Where x.x.x is the desired library version.

2。设置客户端

例如,如果要将超时设置为60秒,请在版本2之前进行Retrofit,然后在版本3之前使用Okhttp( 对于新版本,请参阅编辑 ):

For example, if you want to set a timeout of 60 seconds, do this way for Retrofit before version 2 and Okhttp before version 3 (FOR THE NEWER VERSIONS, SEE THE EDITS):

public RestAdapter providesRestAdapter(Gson gson) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);

    return new RestAdapter.Builder()
        .setEndpoint(BuildConfig.BASE_URL)
        .setConverter(new GsonConverter(gson))
        .setClient(new OkClient(okHttpClient))
        .build();
}






编辑1

对于自 3.xx 以来的okhttp版本,您必须以这种方式设置依赖关系:

For okhttp versions since 3.x.x, you have to set the dependency this way:

compile 'com.squareup.okhttp3:okhttp:x.x.x'

使用构建器模式设置客户端:

And set the client using the builder pattern:

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .build();

超时

编辑2

2.xx 以来的改造版本也使用构建器模式,因此将上面的返回块更改为:

Retrofit versions since 2.x.x also uses the builder pattern, so change the return block above to this:

return new Retrofit.Builder()
    .baseUrl(BuildConfig.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .client(okHttpClient)
    .build();

如果使用像我的 provideRestAdapter 方法这样的代码,然后将方法返回类型更改为 Retrofit

If using a code like my providesRestAdapter method, then change the method return type to Retrofit.

Retrofit 2 - 中的更多信息 - 升级指南从1.9

ps:如果你的minSdkVersion大于8,你可以使用 TimeUnit.MINUTES

ps: If your minSdkVersion is greater than 8, you can use TimeUnit.MINUTES:

okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);

有关单位的更多详细信息,请参阅 TimeUnit

For more details about the units, see TimeUnit.

这篇关于如何在Retrofit库中设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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