How to set connection timeout with OkHttp

后端 未结 11 845
一向
一向 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:47

    For Retrofit 2.0.0-beta1 or beta2, the code goes as follows:

    OkHttpClient client = new OkHttpClient();
    
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.yourapp.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();
    

提交回复
热议问题