How to set connection timeout with OkHttp

后端 未结 11 902
一向
一向 2020-11-27 10:32

I am developing app using OkHttp library and my trouble is I cannot find how to set connection timeout and socket timeout.

OkHttpClient client = new OkHttpCl         


        
11条回答
  •  孤独总比滥情好
    2020-11-27 10:50

    For Retrofit retrofit:2.0.0-beta4 the code goes as follows:

    OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(logging)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build();
    
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.yourapp.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();
    

提交回复
热议问题