Class OkHttpClient
- All Implemented Interfaces:
URLStreamHandlerFactory
public final class OkHttpClient extends Object implements URLStreamHandlerFactory
-
Constructor Summary
Constructors Constructor Description OkHttpClient() -
Method Summary
Modifier and Type Method Description voidcancel(Object tag)Cancels all scheduled tasks tagged withtag.URLStreamHandlercreateURLStreamHandler(String protocol)Creates a URLStreamHandler as aURL.setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory).voidenqueue(Request request, Response.Receiver responseReceiver)Schedulesrequestto be executed.OkAuthenticatorgetAuthenticator()ConnectionPoolgetConnectionPool()intgetConnectTimeout()Default connect timeout (in milliseconds).CookieHandlergetCookieHandler()booleangetFollowProtocolRedirects()HostnameVerifiergetHostnameVerifier()OkResponseCachegetOkResponseCache()ProxygetProxy()ProxySelectorgetProxySelector()intgetReadTimeout()Default read timeout (in milliseconds).ResponseCachegetResponseCache()RouteDatabasegetRoutesDatabase()SSLSocketFactorygetSslSocketFactory()List<String>getTransports()HttpURLConnectionopen(URL url)OkHttpClientsetAuthenticator(OkAuthenticator authenticator)Sets the authenticator used to respond to challenges from the remote web server or proxy server.OkHttpClientsetConnectionPool(ConnectionPool connectionPool)Sets the connection pool used to recycle HTTP and HTTPS connections.voidsetConnectTimeout(long timeout, TimeUnit unit)Sets the default connect timeout for new connections.OkHttpClientsetCookieHandler(CookieHandler cookieHandler)Sets the cookie handler to be used to read outgoing cookies and write incoming cookies.OkHttpClientsetFollowProtocolRedirects(boolean followProtocolRedirects)Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.OkHttpClientsetHostnameVerifier(HostnameVerifier hostnameVerifier)Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.OkHttpClientsetProxy(Proxy proxy)Sets the HTTP proxy that will be used by connections created by this client.OkHttpClientsetProxySelector(ProxySelector proxySelector)Sets the proxy selection policy to be used if noproxyis specified explicitly.voidsetReadTimeout(long timeout, TimeUnit unit)Sets the default read timeout for new connections.OkHttpClientsetResponseCache(ResponseCache responseCache)Sets the response cache to be used to read and write cached responses.OkHttpClientsetSslSocketFactory(SSLSocketFactory sslSocketFactory)Sets the socket factory used to secure HTTPS connections.OkHttpClientsetTransports(List<String> transports)Configure the transports used by this client to communicate with remote servers.
-
Constructor Details
-
OkHttpClient
public OkHttpClient()
-
-
Method Details
-
setConnectTimeout
Sets the default connect timeout for new connections. A value of 0 means no timeout.- See Also:
URLConnection.setConnectTimeout(int)
-
getConnectTimeout
public int getConnectTimeout()Default connect timeout (in milliseconds). -
setReadTimeout
Sets the default read timeout for new connections. A value of 0 means no timeout.- See Also:
URLConnection.setReadTimeout(int)
-
getReadTimeout
public int getReadTimeout()Default read timeout (in milliseconds). -
setProxy
Sets the HTTP proxy that will be used by connections created by this client. This takes precedence oversetProxySelector(java.net.ProxySelector), which is only honored when this proxy is null (which it is by default). To disable proxy use completely, callsetProxy(Proxy.NO_PROXY). -
getProxy
-
setProxySelector
Sets the proxy selection policy to be used if noproxyis 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 defaultproxy selector will be used. -
getProxySelector
-
setCookieHandler
Sets the cookie handler to be used to read outgoing cookies and write incoming cookies.If unset, the
system-wide defaultcookie handler will be used. -
getCookieHandler
-
setResponseCache
Sets the response cache to be used to read and write cached responses.If unset, the
system-wide defaultresponse cache will be used. -
getResponseCache
-
getOkResponseCache
-
setSslSocketFactory
Sets the socket factory used to secure HTTPS connections.If unset, the
system-wide defaultSSL socket factory will be used. -
getSslSocketFactory
-
setHostnameVerifier
Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.If unset, the
system-wide defaulthostname verifier will be used. -
getHostnameVerifier
-
setAuthenticator
Sets the authenticator used to respond to challenges from the remote web server or proxy server.If unset, the
system-wide defaultauthenticator will be used. -
getAuthenticator
-
setConnectionPool
Sets the connection pool used to recycle HTTP and HTTPS connections.If unset, the
system-wide defaultconnection pool will be used. -
getConnectionPool
-
setFollowProtocolRedirects
Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.If unset, protocol redirects will be followed. This is different than the built-in
HttpURLConnection's default. -
getFollowProtocolRedirects
public boolean getFollowProtocolRedirects() -
getRoutesDatabase
-
setTransports
Configure the transports used by this client to communicate with remote servers. By default this client will prefer the most efficient transport available, falling back to more ubiquitous transports. Applications should only call this method to avoid specific compatibility problems, such as web servers that behave incorrectly when SPDY is enabled.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.
- Parameters:
transports- the transports to use, in order of preference. The list must contain "http/1.1". It must not contain null.
-
getTransports
-
enqueue
Schedulesrequestto be executed. -
cancel
Cancels all scheduled tasks tagged withtag. Requests that are already in flight might not be canceled. -
open
-
createURLStreamHandler
Creates a URLStreamHandler as aURL.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);- Specified by:
createURLStreamHandlerin interfaceURLStreamHandlerFactory- Parameters:
protocol- the protocol for which a handler is needed.- Returns:
- the created handler.
-