gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:42 38 4
gpt4 key购买 nike

我在 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com