OkHttp3-使用详解-OkHttpClient

首先附上官网地址:http://square.github.io/okhttp/3.x/okhttp/(点击传送官网,如果进不去,请自行翻墙)
翻译官网:

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.
Call.Factory可以发送一个HTTP请求和读取返回的响应。
OkHttpClients should be shared
OkHttpClient应该被共享。
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.
OkHttp最好的使用时创建一个单列的OkHttpClient 和重复使用它为你应用中的所有的请求。
这是因为不同的客户端持有自己的连接池和线程池。重使用连接和线程可以减少延迟和节省内存。相对地,在空闲池中为每一个请求创建一个客户端是浪费资源的。
Use new OkHttpClient() to create a shared instance with the default settings:
使用 new OkHttpClient()创建一个默认设置共享的实例

 public final OkHttpClient client = new OkHttpClient();

Or use new OkHttpClient.Builder() to create a shared instance with custom settings:
或者是使用new OkHttpClient.Builder()创建一个自定义设置的实例。

 // 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()
使用newBuilder()定制自己的client
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.
你可以使用newBuilder()共享一个OkHttpClient实例,这将建立一个具有相同的 connection pool, thread pools, 和configuration的客户端。使用builder方法去配置一个具有特殊目的的client。
This example shows a call with a short 500 millisecond timeout:
这个例子展示了一个500毫秒的超时

  OkHttpClient eagerClient = client.newBuilder()
       .readTimeout(500, TimeUnit.MILLISECONDS)
       .build();
   Response response = eagerClient.newCall(request).execute();

Shutdown isn’t necessary
Shutdown不是必须的。
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.
关闭dispatcher’s executor service使用shutdown()。这也会导致将来对客户端的调用被拒绝。

client.dispatcher().executorService().shutdown();

Clear the connection pool with evictAll(). Note that the connection pool’s daemon thread may not exit immediately.
清除连接池使用evictAll().注意这个连接池的守护线程不会立马退出。

 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().注意这里有一个错误,创建一个calls去反对它的关闭将会引起crash。

 client.cache().close();

OkHttp also uses daemon threads for HTTP/2 connections. These will exit automatically if they remain idle.

OkHttp也使用守护线程去HTTP/2 连接。如果他们保持空闲这个将会自动退出。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值