简体   繁体   中英

OkHttp Per Request Timeout Settting?

Click Here To show my issue ,I just want set per request timeout and other paramters, so just use:

<!-- language: lang-java -->

 private static final OkHttpClient globalOkHttpClient = new OkHttpClient.Builder().build();
 private static final OkHttpClient otherOkHttpClient10 = globalOkHttpClient.newBuilder()
.connectTimeout(10,TimeUnit.SECONDS)
.build();
 private static final OkHttpClient otherOkHttpClient20 = globalOkHttpClient.newBuilder()
.connectTimeout(20,TimeUnit.SECONDS)
.build();

But that may not reusing a single OkHttp instance? So,I just want to ask?How to set OkHttpClient timeout Per Request . My OkHttp3 maven dependency:

<!-- language: lang-xml -->

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.9.0</version>
</dependency>

Separate from your issue but worth noting, the connect timeout is defaulted to 10 seconds. so you can use a single client here.

If this is the case you shouldn't have 500k OkHttp threads. perhaps debug where each thread is started. It shouldn't be by default.

If you need per request settings, you can make what is effectively a shallow copy

https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PerCallSettings.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM