Class OkHttpClient

  • All Implemented Interfaces:
    Cloneable, Call.Factory


    public class OkHttpClient
    extends Object
    implements Cloneable, Call.Factory
    Factory for calls, which can be used to send HTTP requests and read their responses. Most applications can use a single OkHttpClient for all of their HTTP requests, benefiting from a shared response cache, thread pool, connection re-use, etc.

    To create an OkHttpClient with the default settings, use the default constructor. Or create a configured instance with OkHttpClient.Builder. To adjust an existing client before making a request, use newBuilder(). This example shows a call with a 30 second timeout:

        OkHttpClient client = ... OkHttpClient clientWith30sTimeout = client.newBuilder() .readTimeout(30, TimeUnit.SECONDS) .build(); Response response = clientWith30sTimeout.newCall(request).execute(); 
    • Constructor Detail

      • OkHttpClient

        public OkHttpClient()
    • Method Detail

      • connectTimeoutMillis

        public int connectTimeoutMillis()
        Default connect timeout (in milliseconds).
      • readTimeoutMillis

        public int readTimeoutMillis()
        Default read timeout (in milliseconds).
      • writeTimeoutMillis

        public int writeTimeoutMillis()
        Default write timeout (in milliseconds).
      • proxy

        public Proxy proxy()
      • cache

        public Cache cache()
      • dns

        public Dns dns()
      • followSslRedirects

        public boolean followSslRedirects()
      • followRedirects

        public boolean followRedirects()
      • retryOnConnectionFailure

        public boolean retryOnConnectionFailure()
      • interceptors

        public 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).
      • networkInterceptors

        public List<Interceptor> networkInterceptors()
        Returns an immutable list of interceptors that observe a single network request and response. These interceptors must call Interceptor.Chain.proceed(okhttp3.Request) exactly once: it is an error for a network interceptor to short-circuit or repeat a network request.
      • newCall

        public Call newCall(Request request)
        Prepares the request to be executed at some point in the future.