An HTTP & HTTP/2 client for Android and Java applications 用于Android和Java应用程序HTTP和HTTP/2的客户端
Overview 描述
HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.
HTTP是拉近网络应用程序的方式.这是我们如何交换数据和媒体.使用HTTP有效地使你的东西加载更快,节省带宽.
OkHttp is an HTTP client that’s efficient by default:
- HTTP/2 support allows all requests to the same host to share a socket.
- Connection pooling reduces request latency (if HTTP/2 isn’t available).
- Transparent GZIP shrinks download sizes.
- Response caching avoids the network completely for repeat requests.
OkHttp就是一个有效的HTTP客户端:
- HTTP/2的支持允许所有请求在相同的主机上共享一个socket.
- 连接池可以减少请求延迟(如果HTTP/2则不可使用).
- 透明的压缩可以减少下载大小.
- 响应缓存避免了网络重复完整的请求.
OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.
当网络出现问题时OkHttp会坚守着:它会悄然从常见的连接问题中恢复过来.如果你的服务有多个IP地址,如果第一个连接失败OkHttp将尝试备用地址.对IPv4 + IPv6和服务来说托管在冗余的数据中心是必要的.OkHttp发起最新TLS特性(SNI ALPN)的新连接,如果握手失败则回退至TLS 1.0.
Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.
使用OkHttp很容易.它的请求/响应的API设计拥有流畅的建造器和不变性.它同时支持同步阻塞调用和异步回调调用.
OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.
OkHttp支持Android 2.3及以上.对于Java,最低要求是1.7.
对OkHttp感兴趣的朋友可以看一看Okhttp-wiki系列,可以帮助你理解Okhttp的使用方法及原理:
- Okhttp-wiki 之 Home 主页
- Okhttp-wiki 之 Calls 调用
- Okhttp-wiki 之 Connections 连接
- Okhttp-wiki 之 Recipes 秘诀(食谱)
- Okhttp-wiki 之 Interceptors 拦截器
- Okhttp-wiki 之 HTTPS
Download 下载
Okhttp's Latest JAR
You'll also need Okio, which OkHttp uses for fast I/O and resizable buffers. Download the latest JAR.
你将还需要 Okio,OkHttp用来快速I/O和可调整大小的缓冲区.下载最新的JAR。
The source code to OkHttp, its samples, and this website is available on GitHub.
OkHttp的源代码,其示例,可以在这个网站上看到: GitHub.
MAVEN
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>(insert latest version)</version>
</dependency>
GRADLE
compile 'com.squareup.okhttp3:okhttp:(insert latest version)'