问题
我在我的应用程序中使用Retrofitlibrary,我想设置60秒的超时。 Retrofit有办法做到这一点吗?
我这样设置Retrofit:
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer(BuildConfig.BASE_URL)
.setConverter(new GsonConverter(gson))
.build();
如何设置超时?
#1 热门回答(235 赞)
你可以在底层HTTP客户端上设置超时。如果你未指定客户端,则Retrofit将创建一个具有默认连接和读取超时的客户端。要设置自己的超时,你需要配置自己的客户端并将其提供给RestAdapter.Builder。
一个选项是使用也来自Square的OkHttp客户端。
1.添加库依赖关系
在build.gradle中,包含以下行:
compile 'com.squareup.okhttp:okhttp:x.x.x'
Wherex.x.x是所需的库版本。
2.设置客户端
例如,如果要将超时设置为60秒,请在版本2之前进行Retrofit,然后在版本3之前执行Okhttp(对于更新版本,请参阅编辑):
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.x.x以来的okhttp版本,你必须以这种方式设置依赖关系:
compile 'com.squareup.okhttp3:okhttp:x.x.x'
并使用构建器模式设置客户端:
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
更多信息inTimeouts
编辑2
自2.x.x以来的改造版本也使用构建器模式,因此将上面的返回块更改为:
return new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
如果使用类似myprovidesRestAdapter方法的代码,则将方法返回类型更改为Retrofit。
ps:如果你的minSdkVersion大于8,你可以使用465656527:
okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);
有关单位的更多详细信息,请参阅TimeUnit。
#2 热门回答(45 赞)
这些答案对我来说已经过时了,所以这就是它如何解决的问题。
添加OkHttp,在我的情况下版本是3.3.1:
compile 'com.squareup.okhttp3:okhttp:3.3.1'
然后在构建你的Retrofit之前,执行以下操作:
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
#3 热门回答(1 赞)
我正在使用Retrofit 1.9获得aXML。
public class ServicioConexionRetrofitXML {
public static final String API_BASE_URL = new GestorPreferencias().getPreferencias().getHost();
public static final long tiempoMaximoRespuestaSegundos = 60;
public static final long tiempoMaximoLecturaSegundos = 100;
public static final OkHttpClient clienteOkHttp = new OkHttpClient();
private static RestAdapter.Builder builder = new RestAdapter.Builder().
setEndpoint(API_BASE_URL).
setClient(new OkClient(clienteOkHttp)).setConverter(new SimpleXMLConverter());
public static S createService(Class serviceClass) {
clienteOkHttp.setConnectTimeout(tiempoMaximoRespuestaSegundos, TimeUnit.SECONDS);
clienteOkHttp.setReadTimeout(tiempoMaximoLecturaSegundos, TimeUnit.SECONDS);
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
}
}
如果你使用的是Retrofit 1.9.0和okhttp 2.6.0,请添加到你的Gradle文件中。
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.6.0'
// Librería de retrofit para XML converter (Simple) Se excluyen grupos para que no entre
// en conflicto.
compile('com.squareup.retrofit:converter-simplexml:1.9.0') {
exclude group: 'xpp3', module: 'xpp3'
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
}
注意:如果你需要获取aJSON,只需从上面的代码中删除即可。
.setConverter(new SimpleXMLConverter())