public final class OkHttpClient extends Object implements URLStreamHandlerFactory
| Constructor and Description |
|---|
OkHttpClient() |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel(Object tag)
Cancels all scheduled tasks tagged with
tag. |
URLStreamHandler |
createURLStreamHandler(String protocol)
Creates a URLStreamHandler as a
URL.setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory). |
OkAuthenticator |
getAuthenticator() |
ConnectionPool |
getConnectionPool() |
int |
getConnectTimeout()
Default connect timeout (in milliseconds).
|
CookieHandler |
getCookieHandler() |
boolean |
getFollowProtocolRedirects() |
HostnameVerifier |
getHostnameVerifier() |
OkResponseCache |
getOkResponseCache() |
Proxy |
getProxy() |
ProxySelector |
getProxySelector() |
int |
getReadTimeout()
Default read timeout (in milliseconds).
|
ResponseCache |
getResponseCache() |
RouteDatabase |
getRoutesDatabase() |
SSLSocketFactory |
getSslSocketFactory() |
List<String> |
getTransports() |
HttpURLConnection |
open(URL url) |
OkHttpClient |
setAuthenticator(OkAuthenticator authenticator)
Sets the authenticator used to respond to challenges from the remote web
server or proxy server.
|
OkHttpClient |
setConnectionPool(ConnectionPool connectionPool)
Sets the connection pool used to recycle HTTP and HTTPS connections.
|
void |
setConnectTimeout(long timeout,
TimeUnit unit)
Sets the default connect timeout for new connections.
|
OkHttpClient |
setCookieHandler(CookieHandler cookieHandler)
Sets the cookie handler to be used to read outgoing cookies and write
incoming cookies.
|
OkHttpClient |
setFollowProtocolRedirects(boolean followProtocolRedirects)
Configure this client to follow redirects from HTTPS to HTTP and from HTTP
to HTTPS.
|
OkHttpClient |
setHostnameVerifier(HostnameVerifier hostnameVerifier)
Sets the verifier used to confirm that response certificates apply to
requested hostnames for HTTPS connections.
|
OkHttpClient |
setProxy(Proxy proxy)
Sets the HTTP proxy that will be used by connections created by this
client.
|
OkHttpClient |
setProxySelector(ProxySelector proxySelector)
Sets the proxy selection policy to be used if no
proxy
is specified explicitly. |
void |
setReadTimeout(long timeout,
TimeUnit unit)
Sets the default read timeout for new connections.
|
OkHttpClient |
setResponseCache(ResponseCache responseCache)
Sets the response cache to be used to read and write cached responses.
|
OkHttpClient |
setSslSocketFactory(SSLSocketFactory sslSocketFactory)
Sets the socket factory used to secure HTTPS connections.
|
OkHttpClient |
setTransports(List<String> transports)
Configure the transports used by this client to communicate with remote
servers.
|
public void setConnectTimeout(long timeout,
TimeUnit unit)
URLConnection.setConnectTimeout(int)public int getConnectTimeout()
public void setReadTimeout(long timeout,
TimeUnit unit)
URLConnection.setReadTimeout(int)public int getReadTimeout()
public OkHttpClient setProxy(Proxy proxy)
setProxySelector(java.net.ProxySelector), which is
only honored when this proxy is null (which it is by default). To disable
proxy use completely, call setProxy(Proxy.NO_PROXY).public Proxy getProxy()
public OkHttpClient setProxySelector(ProxySelector proxySelector)
proxy
is specified explicitly. The proxy selector may return multiple proxies;
in that case they will be tried in sequence until a successful connection
is established.
If unset, the system-wide default
proxy selector will be used.
public ProxySelector getProxySelector()
public OkHttpClient setCookieHandler(CookieHandler cookieHandler)
If unset, the system-wide default
cookie handler will be used.
public CookieHandler getCookieHandler()
public OkHttpClient setResponseCache(ResponseCache responseCache)
If unset, the system-wide default
response cache will be used.
public ResponseCache getResponseCache()
public OkResponseCache getOkResponseCache()
public OkHttpClient setSslSocketFactory(SSLSocketFactory sslSocketFactory)
If unset, the system-wide default SSL socket factory will be used.
public SSLSocketFactory getSslSocketFactory()
public OkHttpClient setHostnameVerifier(HostnameVerifier hostnameVerifier)
If unset, the system-wide default hostname verifier will be used.
public HostnameVerifier getHostnameVerifier()
public OkHttpClient setAuthenticator(OkAuthenticator authenticator)
If unset, the system-wide default
authenticator will be used.
public OkAuthenticator getAuthenticator()
public OkHttpClient setConnectionPool(ConnectionPool connectionPool)
If unset, the system-wide
default connection pool will be used.
public ConnectionPool getConnectionPool()
public OkHttpClient setFollowProtocolRedirects(boolean followProtocolRedirects)
If unset, protocol redirects will be followed. This is different than
the built-in HttpURLConnection's default.
public boolean getFollowProtocolRedirects()
public RouteDatabase getRoutesDatabase()
public OkHttpClient setTransports(List<String> transports)
The following transports are currently supported:
This is an evolving set. Future releases may drop support for transitional transports (like spdy/3), in favor of their successors (spdy/4 or http/2.0). The http/1.1 transport will never be dropped.
If multiple protocols are specified, NPN will be used to negotiate a transport. Future releases may use another mechanism (such as ALPN) to negotiate a transport.
transports - the transports to use, in order of preference. The list
must contain "http/1.1". It must not contain null.public void cancel(Object tag)
tag. Requests that are already
in flight might not be canceled.public HttpURLConnection open(URL url)
public URLStreamHandler createURLStreamHandler(String protocol)
URL.setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory).
This code configures OkHttp to handle all HTTP and HTTPS connections
created with URL.openConnection():
OkHttpClient okHttpClient = new OkHttpClient();
URL.setURLStreamHandlerFactory(okHttpClient);
createURLStreamHandler in interface URLStreamHandlerFactoryCopyright © 2014. All rights reserved.