【问题标题】:Does Retrofit or OkHttp limit number of parallel requests?Retrofit 或 OkHttp 是否限制并行请求的数量?
【发布时间】:2021-01-18 04:23:46
【问题描述】:

我使用 RxJavaCallAdapterFactory.create()Schedulers.io(),所以没有使用 Dispatcher 并且 maxRequests 和 maxRequestsPerHost 不是问题。 Schedulers.io() 创建无限数量的线程。

问题是当我运行 10 个请求,然后再运行 1 个请求,而之前没有完成这 1 个请求需要更长的时间才能启动。如果 10 个请求完成,则立即运行下一个请求。 当我有几个请求运行时,我在日志中看到 Call.execute() 被调用,然后请求仅在 6 秒后开始。当没有活动请求时,新请求立即开始。那么是android的限制还是一些网络库的限制?

【问题讨论】:

    标签: retrofit retrofit2 okhttp


    【解决方案1】:

    是的,在 OkHttp 的 Dispatcher 中进行配置。

    【讨论】:

      【解决方案2】:

      使用OkHttp Dispatcher

      Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
      dispatcher.setMaxRequests(20);
      dispatcher.setMaxRequestsPerHost(1);
      
      OkHttpClient okHttpClient = new OkHttpClient.Builder()
         .dispatcher(dispatcher)
         .build();
      
      Retrofit retrofit = new Retrofit.Builder()
         .baseUrl(HttpUrl.get("https://example.com/"))
         .client(okHttpClient)
         .build();
      

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        • 2021-10-15
        • 2018-01-02
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多