Shite title because it's 2 errors both are IlegalState:
So error 1 is
Process: dk.swibbid.mz, PID: 11764
java.lang.IllegalStateException: cannot make a new request because the previous response is still open: please call response.close()
at okhttp3.internal.connection.RealCall.enterNetworkInterceptorExchange(RealCall.kt:229)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:66)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at dk.swibbid.mz.api.RetrofitClient$requestInfoInterceptor$1.intercept(RetrofitClient.kt:50)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at dk.swibbid.mz.api.RetrofitClient$authInterceptor$1.intercept(RetrofitClient.kt:38)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:221)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
So i tried to close all my interseptors responses by like this:
class RetrofitClient {
private val BASE_URL = "xxxx"
private val USER_AGENT = "Android App"
private val gson = GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create()
private val tokenStorage: PreferenceTokenStorage
val client: Retrofit
init {
tokenStorage = PreferenceTokenStorage()
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
val authInterceptor = label@ Interceptor { chain: Interceptor.Chain ->
val originalRequest = chain.request()
if (tokenStorage.hasToken()) {
val request = originalRequest
.newBuilder()
.addHeader(AUTHORIZATION, tokenStorage.accessToken!!)
.build()
chain.proceed(request).also {
it.close()
}
}
chain.proceed(originalRequest).also {
it.close()
}
}
val requestInfoInterceptor = Interceptor { chain: Interceptor.Chain ->
val originalRequest = chain.request()
val requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", USER_AGENT)
.header("App-Version", BuildConfig.VERSION_NAME)
.build()
chain.proceed(requestWithUserAgent).also {
it.close()
}
}
val httpClient = OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(authInterceptor)
.addInterceptor(requestInfoInterceptor)
.authenticator(
TokenAuthenticator(tokenStorage)
)
client = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BASE_URL)
.client(httpClient.build())
.build()
}
}
But doing this gives me this error:
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.request(RealBufferedSource.kt:203)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:247)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
I couldn't find any answers anywhere and i've also tried to just close only one interceptor at a time but that don't work either. I have being sitting for hours trying to find an answer. I need some help here.
this is the two functions im calling from my view model:
fun registerUser(userData: UserData){
if (networkCheck()) return
viewModelScope.launch(Dispatchers.IO){
userDataRepository.insertUserData(userData).enqueue(object : Callback<BaseResponse> {
override fun onResponse(
call: Call<BaseResponse>,
response: Response<BaseResponse>
) {
if (response.isSuccessful) {
response.body()
} else {
Toast.makeText(
getApplication(),
"Error: " + (response.body()?.errorResponse
?: getApplication<SwibbidApplication>().getString(
R.string.error_unknown
)),
Toast.LENGTH_SHORT
).show()
}
}
override fun onFailure(call: Call<BaseResponse>, t: Throwable) {
Toast.makeText(
getApplication(),
"Error: " + getApplication<SwibbidApplication>().getString(R.string.error_network_failed),
Toast.LENGTH_SHORT
).show()
}
})
}
}
fun registerWithProfilePicture(part: MultipartBody.Part,userData: UserData){
if (networkCheck()) return
viewModelScope.launch(Dispatchers.IO){
userDataRepository.uploadProfilePicture(part).enqueue(object :
Callback<LinkResponse> {
override fun onResponse(
call: Call<LinkResponse>,
response: Response<LinkResponse>
) {
if (response.isSuccessful) {
userData.photoUrl = response.body()?.links?.get(0)
registerUser(userData)
} else {
Toast.makeText(
getApplication(),
"Error: " + (response.body()?.error
?: getApplication<SwibbidApplication>().getString(
R.string.error_unknown
)),
Toast.LENGTH_SHORT
).show()
}
}
override fun onFailure(call: Call<LinkResponse>, t: Throwable) {
Toast.makeText(getApplication(), "Error: " + getApplication<SwibbidApplication>().getString(R.string.error_network_failed), Toast.LENGTH_SHORT).show()
}
})
}
}