How to set timeout in Retrofit-2.0+ android

前端 未结 4 1874
甜味超标
甜味超标 2021-02-01 01:19

I referred this link but I can\'t seem to implement for mine

I am using

 compile \'com.squareup.retrofit2:retrofit:2.0         


        
4条回答
  •  一向
    一向 (楼主)
    2021-02-01 02:10

    Configure OkHttpClient for timeout option. Then use this as client for Retrofit.Builder.

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

    Use this okHttpClient for Retrofit#Builder

    Retrofit.Builder()
        .client(okHttpClient);
    

    Official OkHttp documentation about timeout is here

提交回复
热议问题