【发布时间】:2016-11-27 07:16:19
【问题描述】:
考虑以下 OkHttp 和 Retrofit 的初始化:
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()
}
在测试其余调用时,我注意到 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 调度程序有关。
这是一个错误吗?还是我错过了什么?
我正在使用 okhttp 3.4.1,并改造 2.1.0。
【问题讨论】:
-
你检查过你创建了多少个http客户端吗?
标签: android rx-java retrofit2 okhttp3