简体   繁体   中英

How can I make concurrent requests faster with Retrofit / OkHttp?

I need to make 50 http GET requests as fast as possible with Retrofit in Android. I am using Retrofit with OkHttp. Currently Retrofit is doing a poor job vs plain Java ThreadPoolExecutor and HttpUrlConnection : about 50sec for Retrofit and 30sec for plain HttpUrlConnection for all 50 requests, if I set the pool size 20 for ThreadPoolExecutor and for Retrofit / OkHttp I set okHttpClient.dispatcher().setMaxRequests(20); .

If I look at logcat I can see that Retrofit is doing maximum of 5 concurrent requests no matter what I set in setMaxRequests() while with ThreadPoolExecutor there are as many concurrent requests as there are available worker threads.

Is there anything I can do to make Retrofit faster? I don't want to switch to HttpUrlConnection because Retrofit is so elegant and easy to use.

Edit 1

I tried providing custom ThreadPoolExecutor to OkHttp but no time improvement from this:

OkHttpClient.Builder builder = new OkHttpClient.Builder();
ExecutorService exec = new ThreadPoolExecutor(20, 20, 1, TimeUnit.HOURS, new LinkedBlockingQueue<>());
Dispatcher d = new Dispatcher(exec);
builder.dispatcher(d);
OkHttpClient okHttpClient = builder.build();
okHttpClient.dispatcher().setMaxRequests(20);

Edit 2

I make all requests to the same endpoint, if this matters

Since they are all going to the same host, have you tried:

okHttpClient.dispatcher().setMaxRequestsPerHost(20);

Dispatcher.setMaxRequestsPerHost

both dispatcher.maxRequests = 20 and dispatcher.maxRequestsPerHost = 20 did the job for me

With some Kotlin example

package com.package



/**
 * Created by touhid on 24/Jan/2022.
 */
object NetworkService {

    private val interceptor = HttpLoggingInterceptor().apply {
        apply { level = HttpLoggingInterceptor.Level.BODY }
    }
    private val okClient = OkHttpClient.Builder()
        .addInterceptor { chain ->

            val token = getToken(chain)
            var newRequest = chain.request().newBuilder()
            token?.let {
                newRequest = newRequest.addHeader("Authorization", "Bearer $it")
                newRequest = newRequest.addHeader("Accept", "application/json")
            }
            chain.proceed(newRequest.build())
        }
        .addInterceptor(interceptor)

        .build().also {
            it.dispatcher.maxRequests = 20
            it.dispatcher.maxRequestsPerHost = 20
        }


    private val retrofit = Retrofit.Builder()
        .client(okClient)
        .baseUrl(Constants.baseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val service: myApiService = retrofit.create(FoodicsApi::class.java)

    

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM