public class HttpClient extends Object implements Cloneable, Call.Factory, WebSocket.Factory
OkHttp performs best when you create a single HttpClient 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 HttpClient() to create a shared instance with the default settings:
// The singleton HTTP client.
public final HttpClient client = new HttpClient();
Or use new HttpClient.Builder() to create a shared instance with custom settings:
// The singleton HTTP client.
public final HttpClient client = new HttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.cache(new Cache(cacheDir, cacheSize))
.build();
You can customize a shared HttpClient 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:
HttpClient 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 |
HttpClient.Builder |
| Constructor and Description |
|---|
HttpClient() |
| Modifier and Type | Method and Description |
|---|---|
Authenticator |
authenticator() |
Cache |
cache() |
int |
callTimeoutMillis() |
CertificatePinner |
certificatePinner() |
ConnectionPool |
connectionPool() |
List<ConnectionSpec> |
connectionSpecs() |
int |
connectTimeoutMillis() |
CookieJar |
cookieJar() |
Dispatcher |
dispatcher() |
Dns |
dns() |
EventListener.Factory |
eventListenerFactory() |
boolean |
followRedirects() |
boolean |
followSslRedirects() |
HostnameVerifier |
hostnameVerifier() |
List<Interceptor> |
interceptors() |
List<Interceptor> |
networkInterceptors() |
HttpClient.Builder |
newBuilder() |
Call |
newCall(Request request) |
WebSocket |
newWebSocket(Request request,
SocketListener listener) |
int |
pingIntervalMillis() |
List<Protocol> |
protocols() |
Proxy |
proxy() |
Authenticator |
proxyAuthenticator() |
ProxySelector |
proxySelector() |
int |
readTimeoutMillis() |
boolean |
retryOnConnectionFailure() |
SocketFactory |
socketFactory() |
SSLSocketFactory |
sslSocketFactory() |
int |
writeTimeoutMillis() |
public int callTimeoutMillis()
public int connectTimeoutMillis()
public int readTimeoutMillis()
public int writeTimeoutMillis()
public int pingIntervalMillis()
public Proxy proxy()
public ProxySelector proxySelector()
public CookieJar cookieJar()
public Cache cache()
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()
public EventListener.Factory eventListenerFactory()
public Call newCall(Request request)
newCall in interface Call.Factorypublic WebSocket newWebSocket(Request request, SocketListener listener)
newWebSocket in interface WebSocket.Factorypublic HttpClient.Builder newBuilder()
Copyright © 2019. All rights reserved.