An HTTP & HTTP/2 client for Android and Java applications 用于Android和Java應(yīng)用程序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是拉近網(wǎng)絡(luò)應(yīng)用程序的方式.這是我們?nèi)绾谓粨Q數(shù)據(jù)和媒體.使用HTTP有效地使你的東西加載更快,節(jié)省帶寬.
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則不可使用).
- 透明的壓縮可以減少下載大小.
- 響應(yīng)緩存避免了網(wǎng)絡(luò)重復(fù)完整的請求.
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.
當(dāng)網(wǎng)絡(luò)出現(xiàn)問題時OkHttp會堅守著:它會悄然從常見的連接問題中恢復(fù)過來.如果你的服務(wù)有多個IP地址,如果第一個連接失敗OkHttp將嘗試備用地址.對IPv4 + IPv6和服務(wù)來說托管在冗余的數(shù)據(jù)中心是必要的.OkHttp發(fā)起最新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很容易.它的請求/響應(yīng)的API設(shè)計擁有流暢的建造器和不變性.它同時支持同步阻塞調(diào)用和異步回調(diào)調(diào)用.
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 調(diào)用
- 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和可調(diào)整大小的緩沖區(qū).下載最新的JAR老玛。
The source code to OkHttp, its samples, and this website is available on GitHub.
OkHttp的源代碼,其示例,可以在這個網(wǎng)站上看到: 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)'