How to increase timeout for retrofit requests in robospice android?

前端 未结 3 890
刺人心
刺人心 2021-02-07 19:02

I have implemented robospice in my project and using retrofit for all api calls. For some of requests, I need to increase timeout, please let me know how can I do that?

3条回答
  •  我寻月下人不归
    2021-02-07 19:29

    I don't know about robospice but with retrofit 2+ and okhttp3+ you should set a new client like this.

    OkHttpClient okHttpClient = new OkHttpClient();
    Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constantes.URL_DOMAIN)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.newBuilder().connectTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).readTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).writeTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).build())
                .build();
    

    import this in your module build.gradle:

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
    

提交回复
热议问题