CRAG技术解析:检索增强生成的动态纠正机制
2026/7/24 11:53:35
OKHttp 对 HTTP/2 多路复用提供了开箱即用的支持,无需手动编写复杂的协议逻辑,核心只需保证「服务端支持 HTTP/2」+「OKHttp 客户端启用 HTTP/2」,即可自动实现单个 TCP 连接上的多请求并发。以下是从「基础使用」到「进阶优化」的完整实现指南:
h2c(HTTP/2 Cleartext)。在 build.gradle 中引入 OKHttp(以 Android 为例):
dependencies { // OKHttp 核心依赖(4.x 版本) implementation 'com.squareup.okhttp3:okhttp:4.12.0' // 可选:日志拦截器(调试用) implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0' }OKHttp 客户端默认开启 HTTP/2,只需创建标准客户端即可:
import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import java.util.concurrent.TimeUnit; // 1. 配置日志拦截器(调试时查看协议版本) HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> { // 打印请求/响应信息,可查看是否使用 HTTP/2 System.out.println("OKHttp Log: " + message); }); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); // 打印头信息(含协议版本) // 2. 创建 OkHttpClient(默认启用 HTTP/2) OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) // 可选:强制启用 HTTP/2(默认已启用,无需配置) // .protocols(Collections.singletonList(Protocol.HTTP_2)) // 可选:禁用 HTTP/1.1(仅保留 HTTP/2)