Почему создание экземпляра OkHttpClient занимает так много времени? - PullRequest
1 голос
/ 13 апреля 2019

У меня есть этот код на устройстве Android 2.3 Gingerbread, которое занимает около 30 секунд.

Я запускаю его в асинхронной задаче вне потока пользовательского интерфейса (но при запуске его не имеет значения)

long startTime = System.nanoTime();
OkHttpClient client = new OkHttpClient.Builder()
        .followRedirects(false)
        .build();
long clientBuilt = System.nanoTime();
Log.d("API", String.format("Created OkHttpClient, it took %f milliseconds", (clientBuilt - startTime)/1000000d));
D/API: Created OkHttpClient, it took 30814.207720 milliseconds

У меня обновленная версия OkHttp для этой версии Android: 'com.squareup.okhttp3:okhttp:3.12.0'

Редактировать:

После применения предложенного Мартином кода: .connectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT))

и выполнения вызова на сервер я получаю исключение:

java.io.IOException: unexpected end of stream on Connection{motoactv.com:443, proxy=DIRECT hostAddress=motoactv.com/69.10.180.44:443 cipherSuite=none protocol=http/1.1}
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        ...
Caused by: java.io.EOFException: \n not found: limit=0 content=…
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:236)
        at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

1 Ответ

0 голосов
/ 13 апреля 2019

Такие старые API пока не поддерживают SNI; приведение в исполнение простого текста HTTP может помочь:

OkHttpClient client = new OkHttpClient.Builder()
    .connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT))
    .followRedirects(false)
    .build();

Если вы войдете в OkHttpClient.Builder, это также скажет, где именно оно застревает.

...