我在Android 2.3 Gingerbread设备上获得了此代码,该过程大约需要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)
最佳答案
这样的旧API尚不支持SNI
;强制使用纯文本HTTP
可能会有所帮助:
OkHttpClient client = new OkHttpClient.Builder()
.connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT))
.followRedirects(false)
.build();
进入
OkHttpClient.Builder
还应该告诉它确切的位置。关于java - 为什么创建OkHttpClient实例需要这么长时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55669483/