OkHttp官方教程

简介: OkHttp官方教程

OkHttp

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 on 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, 2020.


Releases

Our change log has release history.

implementation("com.squareup.okhttp3:okhttp:4.2.1")

Snapshot builds are available. R8 and ProGuard rules are available.


MockWebServer

OkHttp includes a library for testing HTTP, HTTPS, and HTTP/2 clients.

testImplementation("com.squareup.okhttp3:mockwebserver:4.2.1")

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.


目录
相关文章
|
JSON 数据格式
okhttp3 模拟get、post(json参数传递,form表单提交)
本文是博主学习okhttp3 的记录,希望对大家有所帮助。
2847 0
|
负载均衡 监控 NoSQL
Redis系列(三):深入解读Redis主从同步机制(上)
Redis系列(三):深入解读Redis主从同步机制
|
算法 大数据
【MATLAB】鲸鱼算法优化混合核极限学习机(WOA-HKELM)回归预测算法
【MATLAB】鲸鱼算法优化混合核极限学习机(WOA-HKELM)回归预测算法
287 2
|
机器学习/深度学习 应用服务中间件 Linux
API一键搭建智能时光相册,记录你的美
API时代,要搭建一个云相册,就相对来说简单很多,或者说一个开发人员就可以快速实现,并且还能具备智能分析识别、归类、搜索等功能齐全的智能云相册。
4234 0
|
JSON fastjson Java
下个十年高性能 JSON 库来了:fastjson2!
下个十年高性能 JSON 库来了:fastjson2!
1725 0
|
Cloud Native 安全 Anolis
免费、安全、可靠!一站式构建平台 ABS 介绍及实例演示 | 龙蜥技术
一文了解软件包构建、镜像构建、内核源码构建、云原生构建 4 大构建服务。
|
机器学习/深度学习 人工智能 算法
构建未来:人工智能在持续学习系统中的进化
【5月更文挑战第23天】 探索人工智能如何通过模拟生物神经网络和复杂算法,实现自我优化与知识积累。本文将深入分析自学习AI系统的关键技术,包括深度学习、增强学习及未监督学习,并探讨这些技术如何推动AI从静态的知识库向动态的、持续发展的认知实体转变。通过具体案例,揭示自学习系统在处理大数据、进行预测分析以及在自动驾驶、医疗诊断等领域的应用前景。最终讨论了此类系统面临的挑战,如数据隐私、算法偏见和伦理问题,并提出潜在的解决策略。
|
JSON 前端开发 Java
Jackson使用详解
Spring MVC 默认采用Jackson解析Json,尽管还有一些其它同样优秀的json解析工具,例如Fast Json、GSON,但是出于最小依赖的考虑,也许Json解析第一选择就应该是Jackson。
1172 0
Jackson使用详解
|
机器学习/深度学习 编解码 算法
中科大提出PE-YOLO | 让YOLO家族算法直击黑夜目标检测
中科大提出PE-YOLO | 让YOLO家族算法直击黑夜目标检测
338 0
|
数据可视化 关系型数据库 MySQL
使用IDEA连接Mysql数据库
使用IDEA连接Mysql数据库,提前在可视化工具中建好表
918 1

热门文章

最新文章