public class OkHttpClient extends java.lang.Object implements java.lang.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 |
Modifier and Type | Field and Description |
---|---|
(package private) Authenticator |
authenticator |
(package private) Cache |
cache |
(package private) CertificateChainCleaner |
certificateChainCleaner |
(package private) CertificatePinner |
certificatePinner |
(package private) ConnectionPool |
connectionPool |
(package private) java.util.List<ConnectionSpec> |
connectionSpecs |
(package private) int |
connectTimeout |
(package private) CookieJar |
cookieJar |
(package private) static java.util.List<ConnectionSpec> |
DEFAULT_CONNECTION_SPECS |
(package private) static java.util.List<Protocol> |
DEFAULT_PROTOCOLS |
(package private) Dispatcher |
dispatcher |
(package private) Dns |
dns |
(package private) EventListener.Factory |
eventListenerFactory |
(package private) boolean |
followRedirects |
(package private) boolean |
followSslRedirects |
(package private) javax.net.ssl.HostnameVerifier |
hostnameVerifier |
(package private) java.util.List<Interceptor> |
interceptors |
(package private) InternalCache |
internalCache |
(package private) java.util.List<Interceptor> |
networkInterceptors |
(package private) int |
pingInterval |
(package private) java.util.List<Protocol> |
protocols |
(package private) java.net.Proxy |
proxy |
(package private) Authenticator |
proxyAuthenticator |
(package private) java.net.ProxySelector |
proxySelector |
(package private) int |
readTimeout |
(package private) boolean |
retryOnConnectionFailure |
(package private) javax.net.SocketFactory |
socketFactory |
(package private) javax.net.ssl.SSLSocketFactory |
sslSocketFactory |
(package private) int |
writeTimeout |
Constructor and Description |
---|
OkHttpClient() |
OkHttpClient(OkHttpClient.Builder builder) |
Modifier and Type | Method and Description |
---|---|
Authenticator |
authenticator() |
Cache |
cache() |
CertificatePinner |
certificatePinner() |
ConnectionPool |
connectionPool() |
java.util.List<ConnectionSpec> |
connectionSpecs() |
int |
connectTimeoutMillis()
Default connect timeout (in milliseconds).
|
CookieJar |
cookieJar() |
Dispatcher |
dispatcher() |
Dns |
dns() |
EventListener.Factory |
eventListenerFactory() |
boolean |
followRedirects() |
boolean |
followSslRedirects() |
javax.net.ssl.HostnameVerifier |
hostnameVerifier() |
java.util.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).
|
(package private) InternalCache |
internalCache() |
java.util.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 ping interval (in milliseconds).
|
java.util.List<Protocol> |
protocols() |
java.net.Proxy |
proxy() |
Authenticator |
proxyAuthenticator() |
java.net.ProxySelector |
proxySelector() |
int |
readTimeoutMillis()
Default read timeout (in milliseconds).
|
boolean |
retryOnConnectionFailure() |
javax.net.SocketFactory |
socketFactory() |
javax.net.ssl.SSLSocketFactory |
sslSocketFactory() |
private javax.net.ssl.SSLSocketFactory |
systemDefaultSslSocketFactory(javax.net.ssl.X509TrustManager trustManager) |
private javax.net.ssl.X509TrustManager |
systemDefaultTrustManager() |
int |
writeTimeoutMillis()
Default write timeout (in milliseconds).
|
static final java.util.List<Protocol> DEFAULT_PROTOCOLS
static final java.util.List<ConnectionSpec> DEFAULT_CONNECTION_SPECS
final Dispatcher dispatcher
@Nullable final java.net.Proxy proxy
final java.util.List<Protocol> protocols
final java.util.List<ConnectionSpec> connectionSpecs
final java.util.List<Interceptor> interceptors
final java.util.List<Interceptor> networkInterceptors
final EventListener.Factory eventListenerFactory
final java.net.ProxySelector proxySelector
final CookieJar cookieJar
@Nullable final Cache cache
@Nullable final InternalCache internalCache
final javax.net.SocketFactory socketFactory
@Nullable final javax.net.ssl.SSLSocketFactory sslSocketFactory
@Nullable final CertificateChainCleaner certificateChainCleaner
final javax.net.ssl.HostnameVerifier hostnameVerifier
final CertificatePinner certificatePinner
final Authenticator proxyAuthenticator
final Authenticator authenticator
final ConnectionPool connectionPool
final Dns dns
final boolean followSslRedirects
final boolean followRedirects
final boolean retryOnConnectionFailure
final int connectTimeout
final int readTimeout
final int writeTimeout
final int pingInterval
public OkHttpClient()
OkHttpClient(OkHttpClient.Builder builder)
private javax.net.ssl.X509TrustManager systemDefaultTrustManager()
private javax.net.ssl.SSLSocketFactory systemDefaultSslSocketFactory(javax.net.ssl.X509TrustManager trustManager)
public int connectTimeoutMillis()
public int readTimeoutMillis()
public int writeTimeoutMillis()
public int pingIntervalMillis()
public java.net.Proxy proxy()
public java.net.ProxySelector proxySelector()
public CookieJar cookieJar()
public Cache cache()
InternalCache internalCache()
public Dns dns()
public javax.net.SocketFactory socketFactory()
public javax.net.ssl.SSLSocketFactory sslSocketFactory()
public javax.net.ssl.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 java.util.List<Protocol> protocols()
public java.util.List<ConnectionSpec> connectionSpecs()
public java.util.List<Interceptor> interceptors()
public java.util.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()