类 OkHttpClient

java.lang.Object
com.lark.oapi.okhttp.OkHttpClient
所有已实现的接口:
Call.Factory, WebSocket.Factory, Cloneable

public class OkHttpClient extends Object implements Cloneable, Call.Factory, WebSocket.Factory
Factory for calls, which can be used to send HTTP requests and read their responses.

OkHttpClients should be shared

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();
 

Customize your client with newBuilder()

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();
 

Shutdown isn't necessary

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.

  • 构造器详细资料

    • OkHttpClient

      public OkHttpClient()
  • 方法详细资料

    • callTimeoutMillis

      public int callTimeoutMillis()
      Default call timeout (in milliseconds). By default there is no timeout for complete calls, but there is for the connect, write, and read actions within a call.
    • connectTimeoutMillis

      public int connectTimeoutMillis()
      Default connect timeout (in milliseconds). The default is 10 seconds.
    • readTimeoutMillis

      public int readTimeoutMillis()
      Default read timeout (in milliseconds). The default is 10 seconds.
    • writeTimeoutMillis

      public int writeTimeoutMillis()
      Default write timeout (in milliseconds). The default is 10 seconds.
    • pingIntervalMillis

      public int pingIntervalMillis()
      Web socket and HTTP/2 ping interval (in milliseconds). By default pings are not sent.
    • proxy

      @Nullable public Proxy proxy()
    • proxySelector

      public ProxySelector proxySelector()
    • cookieJar

      public CookieJar cookieJar()
    • cache

      @Nullable public Cache cache()
    • dns

      public Dns dns()
    • socketFactory

      public SocketFactory socketFactory()
    • sslSocketFactory

      public SSLSocketFactory sslSocketFactory()
    • hostnameVerifier

      public HostnameVerifier hostnameVerifier()
    • certificatePinner

      public CertificatePinner certificatePinner()
    • authenticator

      public Authenticator authenticator()
    • proxyAuthenticator

      public Authenticator proxyAuthenticator()
    • connectionPool

      public ConnectionPool connectionPool()
    • followSslRedirects

      public boolean followSslRedirects()
    • followRedirects

      public boolean followRedirects()
    • retryOnConnectionFailure

      public boolean retryOnConnectionFailure()
    • dispatcher

      public Dispatcher dispatcher()
    • protocols

      public List<Protocol> protocols()
    • connectionSpecs

      public List<ConnectionSpec> connectionSpecs()
    • 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(com.lark.oapi.okhttp.Request) exactly once: it is an error for a network interceptor to short-circuit or repeat a network request.
    • eventListenerFactory

      public EventListener.Factory eventListenerFactory()
    • newCall

      public Call newCall(Request request)
      Prepares the request to be executed at some point in the future.
      指定者:
      newCall 在接口中 Call.Factory
    • newWebSocket

      public WebSocket newWebSocket(Request request, WebSocketListener listener)
      Uses request to connect a new web socket.
      指定者:
      newWebSocket 在接口中 WebSocket.Factory
    • newBuilder

      public OkHttpClient.Builder newBuilder()