漫长等待改造响应
我在应用程序中使用Raturofit,除了一件事外,一切都还好。在禁用Internet连接后,启用它并向API提出请求(Okhttp Logger表示处理请求已被处理),响应在几分钟内就进行了。当我提出下一个请求时,响应立即出现。
另外,为了缓慢的响应,Okhttp Logger说它已经以约50毫秒的方式处理,但是几分钟后也会打印出登录。
在项目中,我正在使用Koin,但我怀疑这是问题的根源。无论如何,我将改装,OkhttpClient和拦截器定义为单个。
也许会有所帮助,如果我设置和写入超时,例如在Okhttp客户端中5秒钟,我将获得超时异常。
那么,发生了什么以及如何解决该问题?
reatrofit
:
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client) // my OkHttpClient instance
.build()
okhttpclient
:
OkHttpClient.Builder()
.addInterceptor(networkConnectionInterceptor)
.addInterceptor(loggingInterceptor)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build()
networkConnectionInterceptor
和noconnectivityException
classe>类:
class NetworkConnectionInterceptor(private val context: Context) : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
if (!isConnected) {
throw NoConnectivityException()
}
val builder: Request.Builder = chain.request().newBuilder()
return chain.proceed(builder.build())
}
val isConnected: Boolean
get() {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo = connectivityManager.activeNetworkInfo
return netInfo != null && netInfo.isConnected
}
}
class NoConnectivityException : IOException() {
override val message: String
get() = "No Internet Connection"
}
I'm using retrofit in my app and everything is ok except one thing. After disabling internet connection, enabling it back and making request to api (okhttp logger says that GET request was handled), response comes in a few minutes. And when i make next request, response comes immediately.
Also, for the slow response, okhttp logger says that it has been handled in ~50 milliseconds, but prints that log after a few minutes too.
In project i'm using koin, but i doubt it is the root of the problem. Anyway i defined retrofit, okhttpclient and interceptors as single.
And maybe it'll help, if i set read and write timeouts to, for example, 5 seconds in okhttp client i'll get timeout exception.
So what's happening and how to handle with that problem?
Retrofit
:
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client) // my OkHttpClient instance
.build()
OkHttpClient
:
OkHttpClient.Builder()
.addInterceptor(networkConnectionInterceptor)
.addInterceptor(loggingInterceptor)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build()
NetworkConnectionInterceptor
and NoConnectivityException
classes:
class NetworkConnectionInterceptor(private val context: Context) : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
if (!isConnected) {
throw NoConnectivityException()
}
val builder: Request.Builder = chain.request().newBuilder()
return chain.proceed(builder.build())
}
val isConnected: Boolean
get() {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo = connectivityManager.activeNetworkInfo
return netInfo != null && netInfo.isConnected
}
}
class NoConnectivityException : IOException() {
override val message: String
get() = "No Internet Connection"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论