public class OkHttpClient extends Object implements Cloneable, Call.Factory, WebSocket.Factory
OkHttp performs best when you create a single OkHttpClient
instance and reuse it for
all of your HTTP calls. This is because each client holds its own connection pool and thread
pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a
client for each request wastes resources on idle pools.
Use new OkHttpClient()
to create a shared instance with the default settings:
// The singleton HTTP client.
public final OkHttpClient client = new OkHttpClient();
Or use new OkHttpClient.Builder()
to create a shared instance with custom settings:
// The singleton HTTP client.
public final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.cache(new Cache(cacheDir, cacheSize))
.build();
You can customize a shared OkHttpClient instance with newBuilder()
. This builds a
client that shares the same connection pool, thread pools, and configuration. Use the builder
methods to configure the derived client for a specific purpose.
This example shows a call with a short 500 millisecond timeout:
OkHttpClient eagerClient = client.newBuilder()
.readTimeout(500, TimeUnit.MILLISECONDS)
.build();
Response response = eagerClient.newCall(request).execute();
The threads and connections that are held will be released automatically if they remain idle. But if you are writing a application that needs to aggressively release unused resources you may do so.
Shutdown the dispatcher's executor service with shutdown()
.
This will also cause future calls to the client to be rejected.
client.dispatcher().executorService().shutdown();
Clear the connection pool with evictAll()
. Note that the
connection pool's daemon thread may not exit immediately.
client.connectionPool().evictAll();
If your client has a cache, call close()
. Note that it is an error to
create calls against a cache that is closed, and doing so will cause the call to crash.
client.cache().close();
OkHttp also uses daemon threads for HTTP/2 connections. These will exit automatically if they remain idle.
Modifier and Type | Class and Description |
---|---|
static class |
OkHttpClient.Builder |
Constructor and Description |
---|
OkHttpClient() |
Modifier and Type | Method and Description |
---|---|
Authenticator |
authenticator() |
Cache |
cache() |
int |
callTimeoutMillis()
Default call timeout (in milliseconds).
|
CertificatePinner |
certificatePinner() |
ConnectionPool |
connectionPool() |
List<ConnectionSpec> |
connectionSpecs() |
int |
connectTimeoutMillis()
Default connect timeout (in milliseconds).
|
CookieJar |
cookieJar() |
Dispatcher |
dispatcher() |
Dns |
dns() |
EventListener.Factory |
eventListenerFactory() |
boolean |
followRedirects() |
boolean |
followSslRedirects() |
HostnameVerifier |
hostnameVerifier() |
List<Interceptor> |
interceptors()
Returns an immutable list of interceptors that observe the full span of each call: from before
the connection is established (if any) until after the response source is selected (either the
origin server, cache, or both).
|
List<Interceptor> |
networkInterceptors()
Returns an immutable list of interceptors that observe a single network request and response.
|
OkHttpClient.Builder |
newBuilder() |
Call |
newCall(Request request)
Prepares the
request to be executed at some point in the future. |
WebSocket |
newWebSocket(Request request,
WebSocketListener listener)
Uses
request to connect a new web socket. |
int |
pingIntervalMillis()
Web socket and HTTP/2 ping interval (in milliseconds).
|
List<Protocol> |
protocols() |
Proxy |
proxy() |
Authenticator |
proxyAuthenticator() |
ProxySelector |
proxySelector() |
int |
readTimeoutMillis()
Default read timeout (in milliseconds).
|
boolean |
retryOnConnectionFailure() |
SocketFactory |
socketFactory() |
SSLSocketFactory |
sslSocketFactory() |
int |
writeTimeoutMillis()
Default write timeout (in milliseconds).
|
public int callTimeoutMillis()
public int connectTimeoutMillis()
public int readTimeoutMillis()
public int writeTimeoutMillis()
public int pingIntervalMillis()
public ProxySelector proxySelector()
public CookieJar cookieJar()
public Dns dns()
public SocketFactory socketFactory()
public SSLSocketFactory sslSocketFactory()
public HostnameVerifier hostnameVerifier()
public CertificatePinner certificatePinner()
public Authenticator authenticator()
public Authenticator proxyAuthenticator()
public ConnectionPool connectionPool()
public boolean followSslRedirects()
public boolean followRedirects()
public boolean retryOnConnectionFailure()
public Dispatcher dispatcher()
public List<ConnectionSpec> connectionSpecs()
public List<Interceptor> interceptors()
public List<Interceptor> networkInterceptors()
Interceptor.Chain.proceed(okhttp3.Request)
exactly once: it is an error for
a network interceptor to short-circuit or repeat a network request.public EventListener.Factory eventListenerFactory()
public Call newCall(Request request)
request
to be executed at some point in the future.newCall
in interface Call.Factory
public WebSocket newWebSocket(Request request, WebSocketListener listener)
request
to connect a new web socket.newWebSocket
in interface WebSocket.Factory
public OkHttpClient.Builder newBuilder()
Copyright © 2019. All rights reserved.