首先附上官网地址: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 连接。如果他们保持空闲这个将会自动退出。

OkHttp3-使用详解-OkHttpClient相关推荐

  1. OkHttp3使用详解

    引言 最初我们进行HTTP请求时使用的是HttpURLConnection或者HttpClient,那么这两者都有什么优缺点呢? HttpClient是Apache基金会的一个开源网络库,功能十分强大 ...

  2. Android OkHttp3简介和使用详解

    一 OKHttp简介 OKHttp是一个处理网络请求的开源项目,Android 当前最火热网络框架,由移动支付Square公司贡献,用于替代HttpUrlConnection和Apache HttpC ...

  3. OKHttp3的使用和详解

    一.概述 OKHttp是处理网络请求的开源框架,Andorid当前最火热的网络框架,Retrofit的底层也是OKHttp,用于替换HttpUrlConnection和Apache HttpClien ...

  4. OkHttp3源码详解

    前言:为什么有些人宁愿吃生活的苦也不愿吃学习的苦,大概是因为懒惰吧,学习的苦是需要自己主动去吃的,而生活的苦,你躺着不动它就会来找你了. 一.概述 OKHttp是一个非常优秀的网络请求框架,已经被谷歌 ...

  5. OkHttp3源码详解(五) okhttp连接池复用机制

    1.概述 提高网络性能优化,很重要的一点就是降低延迟和提升响应速度. 通常我们在浏览器中发起请求的时候header部分往往是这样的 keep-alive 就是浏览器和服务端之间保持长连接,这个连接是可 ...

  6. Retrofit2 multpart多文件上传详解

    原文出处:http://www.chenkaihua.com/2016/04/02/retrofit2-upload-multipart-files.html Retrofit2是目前很流行的andr ...

  7. Retrofit 注解参数详解

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/121000230 本文出自[赵彦军的博客] 系列文章推荐: Android Flow ...

  8. android OKHttp的基本使用详解

    今天,简单讲讲Android里如何使用OKHttp. Android框架系列: 一.android EventBus的简单使用 二.android Glide简单使用 三.android OKHttp ...

  9. Android OkHttp 全面详解

    Android OkHttp 全面详解 包的导入 基本使用 异步请求 同步请求 build创建 源码跟踪 newCall RealCall.enqueue Dispatcher.enqueue exe ...

最新文章

  1. 建模步骤_Revit软件介绍?Revit参数化构件建模步骤
  2. java和python可以在一个项目中同时使用么-可以同时用python和java开发一个app吗?...
  3. 保驾护航金三银四,万字解析!
  4. 利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序
  5. android数据库给单选赋值,如何使用android studio将单选按钮的值保存到mysql数据库?...
  6. Python将浏览器cookies共享给requests库
  7. TCHAR、WCHAR、CHAR以及WideCharToMultiByte、MultiByteToWideChar
  8. TC中编辑程序快捷键
  9. mysql卸载注意问题_mysql卸载注意事项
  10. 网站快速收录-网站快速收录工具下载免费
  11. 简单好用的桌面隐藏工具:Desktop Curtain for Mac
  12. 非科班转码,上岸小公司我也很满意了
  13. 计算机显卡型号中数字含义详解,显卡型号中字母和数字所代表的含义
  14. qt生成程序以管理员身份运行和取消以管理员身份运行方式
  15. Android 5.1 添加下拉通知栏数据流量开关快捷图标
  16. 一枚钻戒如何成功借势世界杯,与粉丝秀恩爱
  17. 【车载以太网】【SOME/IP】规范标准
  18. 02_Pulsar的集群架构、架构基本介绍、Pulsar提供的组件介绍、Brokers介绍、Zookeeper的元数据存储、基于bookKeeper持久化存储、Pulsar代理
  19. linux下使用代理加速下载方案集合
  20. HTML box盒子模型练习校园风光木棉花

热门文章

  1. linux pdf转视频教程,Linux下PDF操作与转换
  2. JS创建对象模式7种方法详解
  3. 电脑无法访问网页,但qq可以用?一篇文章帮你解决(非常详细)
  4. 如何安装Java JDK
  5. 二维数组vector 限定大小
  6. 解决CentOS7 Development Tools 包不存在问题
  7. 最短路径问题(Dijkstra常用用法总结)
  8. SQL Server 异常 COM 类公司中CLSID 为 {10021F00-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80070005
  9. 【myISAM和innoDB】mySql的引擎myisam和innodb的区别/mysiam(mysql插入速度优化)
  10. C++ QT加载字体,以及iconfont使用