

GitHub - square/okhttp: An HTTP+HTTP/2 client for Android and Java applications.
source link: https://github.com/square/okhttp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

OkHttp
See the project website for documentation and APIs.
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.
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 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 services hosted in redundant data centers. OkHttp supports modern TLS features (TLS 1.3, ALPN, certificate pinning). It can be configured to fall back for broad connectivity.
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.
Get a URL
This program downloads a URL and prints its contents as a string. Full source.
OkHttpClient client = new OkHttpClient(); String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
Post to a Server
This program posts data to a service. Full source.
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(json, JSON); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
Further examples are on the OkHttp Recipes page.
Requirements
OkHttp works on Android 5.0+ (API level 21+) and Java 8+.
OkHttp depends on Okio for high-performance I/O and the Kotlin standard library. Both are small libraries with strong backward-compatibility.
We highly recommend you keep OkHttp up-to-date. As with auto-updating web browsers, staying current with HTTPS clients is an important defense against potential security problems. We track the dynamic TLS ecosystem and adjust OkHttp to improve connectivity and security.
OkHttp uses your platform's built-in TLS implementation. On Java platforms OkHttp also supports Conscrypt, which integrates BoringSSL with Java. OkHttp will use Conscrypt if it is the first security provider:
Security.insertProviderAt(Conscrypt.newProvider(), 1);
The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lack support for TLS 1.2 and should not be used. But because upgrading is difficult, we will backport critical fixes to the 3.12.x branch through December 31, 2021.
Releases
Our change log has release history.
The latest release is available on Maven Central.
implementation("com.squareup.okhttp3:okhttp:4.9.3")
Snapshot builds are available. R8 and ProGuard rules are available.
Also, we have a bill of materials (BOM) available to help you keep OkHttp artifacts up to date and be sure about version compatibility.
dependencies { // define a BOM and its version implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.3")) // define any required OkHttp artifacts without version implementation("com.squareup.okhttp3:okhttp") implementation("com.squareup.okhttp3:logging-interceptor") }
MockWebServer
OkHttp includes a library for testing HTTP, HTTPS, and HTTP/2 clients.
The latest release is available on Maven Central.
testImplementation("com.squareup.okhttp3:mockwebserver:4.9.3")
GraalVM Native Image
Building your native images with Graal https://www.graalvm.org/ should work automatically. This is not currently in a final released version, so 5.0.0-alpha.2 should be used. Please report any bugs or workarounds you find.
See the okcurl module for an example build.
$ ./gradlew okcurl:nativeImage $ ./okcurl/build/graal/okcurl https://httpbin.org/get
License
Copyright 2019 Square, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Recommend
-
277
Retrofit A type-safe HTTP client for Android and Java. For more information please see the website. Download Download
-
162
前提: 没做过网络缓存这方面的功能,所以想学习下。上网看了很多的文章,也看了部分视频。想把自己的一些小小的心德分享一下。如何能够稍微帮助到别人,那对我来说就算是有意义了。废话不多说了。进入正题。1.网路请求 网络请求用的是Retrofit。用过的人
-
156
Today we’re releasing OkHttp 3.13. With this update we’re bumping the project’s requirements from this: Android 2.3+ / API 9+ (released December 2010) Java 7+ (released July 2011)
-
73
前言 RxHttp截止本文发表已经推广了4个礼拜,目前已经有了141个star,如下: 其中一文,Android 史上最优雅的实现文件上传、下载及进度的监听更是得到了大神刘皇叔微信公众号的推送,欢迎读者关注刘皇叔微信公众号「刘望舒」,每天都有精彩的文章推送,
-
67
我们在刷一下面试题的时候,有时候会看到一些大厂会问关于断点续传的原理,那么今天在这里从 HTTP 断点续传知识和 Android 中如何实现断点续传的思路来做一个关于 Android 断点续传原理的总结。 Http 断点续传知识点 什么是断点续传 指的是在上传
-
21
前言 在平时开发中,你有没有下面这样的困扰呢? 场景一 明明是服务端的接口数据错误,而QA(测试)第一个找到的可能是客户端开发的你,为什么这个页面出现错误了? 而作为客户端开发的你,可能要拿出测试机连上电脑,打一下Log,看一下到底返回了什么数据,导致页...
-
16
请求和响应流程 见图。 所有的流程是这个时序图。 整个流程处理的网络...
-
24
OkHttpClinet 1.newCall 产生一个新call,核心是new,应为一个client可以对应很多个call。 从app的生命周期来讲,client可以是单例,call必须一个请求一个,call finish或者calcel以后,就结束了。 2.newWebSocket websocket的...
-
68
How to Test Java HTTP Client Usages (e.g. OkHttp, Apache HttpClient) December 17, 2020 First time here? Get an overview of all topics you'll find answers for on this blog
-
8
Developers use HTTP Clients to communicate with other applications over the network. Over the years, multiple HTTP Clients have been developed to suit various a...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK