How to set connection timeout with OkHttp

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

    If you want to customize the configuration then use the below methodology of creating OKhttpclient first and then add builder on top of it.

    private final OkHttpClient client = new OkHttpClient();
    
    // Copy to customize OkHttp for this request.
    OkHttpClient client1 = client.newBuilder()
        .readTimeout(500, TimeUnit.MILLISECONDS)
        .build();
    try (Response response = client1.newCall(request).execute()) {
        System.out.println("Response 1 succeeded: " + response);
    } catch (IOException e) {
        System.out.println("Response 1 failed: " + e);
    }
    

提交回复
热议问题