考慮OkHttp以下的初始化和改造:Okhttp忽略調度設置
public static SomeServiceRestInterface newRestService(String apiUrl) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(apiUrl)
.client(createOkHttpClient())
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(createGsonConverter())
.build();
return retrofit.create(SomeServiceRestInterface.class);
}
private static OkHttpClient createOkHttpClient() {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost(1);
dispatcher.setMaxRequests(1);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dispatcher(dispatcher).build()
}
當測試REST調用,我注意到,Okhttp根本不兌現setMaxRequestsPerHost或setMaxRequests設置所有。以下是同時發送的3個請求的日誌:
23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - --> POST https://XXX/1 http/1.1
23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - Content-Length: 0
23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - --> END POST (0-byte body)
23/07 04:14:22.672 [RxIoScheduler-7] DEBUG - --> POST https://XXX/2 http/1.1
23/07 04:14:22.673 [RxIoScheduler-7] DEBUG - Content-Length: 0
23/07 04:14:22.673 [RxIoScheduler-7] DEBUG - --> END POST (0-byte body)
23/07 04:14:22.676 [RxIoScheduler-6] DEBUG - --> POST https://XXX/3 http/1.1
23/07 04:14:22.677 [RxIoScheduler-6] DEBUG - Content-Length: 0
23/07 04:14:22.677 [RxIoScheduler-6] DEBUG - --> END POST (0-byte body)
其中XXX是相同的域,1/2/3是不同的路徑。
我不知道爲什麼,但我認爲這可能與在addCallAdapterFactory中設置的RxJava Scheduler有關。
這是一個錯誤?或者我錯過了什麼?
我使用的是okhttp 3.4.1,並且改進了2.1.0。
您是否檢查過您創建了多少個http客戶端? –