如何禁用连接池并为 OkHttp3 中的每个请求建立新连接

How to disable connection pooling and make a new connection for each request in OkHttp3

由于服务器端问题,我们正在尝试禁用 OkHttp 使用的连接池。
OkHttp ConnectionPool class 的初始化程序接收 maxIdleConnections 并保持活动持续时间信息。

public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
  this.delegate = new RealConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
}

public RealConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
  this.maxIdleConnections = maxIdleConnections;
  this.keepAliveDurationNs = timeUnit.toNanos(keepAliveDuration);

  // Put a floor on the keep alive duration, otherwise cleanup will spin loop.
  if (keepAliveDuration <= 0) {
    throw new IllegalArgumentException("keepAliveDuration <= 0: " + keepAliveDuration);
  }
}

maxIdleConnections设置为0可以吗?
我们只需要为每个请求创建一个新连接。

是的,将 maxIdleConnections 设置为 0。