为什么创建OkHttpClient实例需要这么长时间?

问题描述 投票:1回答:1

我在Android 2.3姜饼设备上有这个代码需要30秒。

我在远离UI线程的异步任务中运行它(但它在我运行它的地方没有区别)

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

我有这个Android版本的最新版OkHttp:'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)
java android okhttp3 android-2.3-gingerbread
1个回答
0
投票

这样的旧API还不支持SNI;强制执行纯文本HTTP可能会有所帮助:

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

走进OkHttpClient.Builder也应该说明,它究竟会被困在哪里。

© www.soinside.com 2019 - 2024. All rights reserved.