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

标签 java okhttp

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

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。

关于java - 如何禁用连接池并为 OkHttp3 中的每个请求建立新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70873186/

相关文章:

java - 在单个 JFrame 中使用多个 JPanel

java - 具有两个字符串的基本并行数组

Retrofit2 OkHttp3响应正文为空错误

android - 在 Android 应用程序中下载文件的最佳方式

android - Retrofit + RxJava 无法缓存响应 - Android

java - hashmap java中arraylist的总和值

java - 应用程序无法打开,LogCat 中不断出现以下错误

java - 可序列化集合的通用参数

android - 这个错误java.io.IOException : Content-Length and stream length disagree的原因是什么