错误java.lang.illegalstateexception请求==null



有人能告诉我可能是什么错误吗?有一段时间它运行良好。现在,它出现了,然后消失了。当我开始在自动完成中输入地址时,会出现错误我将非常感激!!

启动时,设备上出现以下错误

java.lang.IllegalStateException: request == null
at okhttp3.Response$Builder.build(Response.java:442)
at com.sheepapps.domras.data.autocomplete.AutocompleteService$client$1.intercept(AutocompleteService.kt:19)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:250)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:201)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
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)

下面的应用程序代码本身

package com.sheepapps.domras.data.autocomplete
import androidx.core.text.isDigitsOnly
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import kotlin.math.pow
class AutocompleteService {
private val client = OkHttpClient.Builder()
.addInterceptor {
try {
it.proceed(it.request())
} catch (e: Exception) {
e.printStackTrace()
Response.Builder().code(500).build()
}
}.build()
private val service = Retrofit.Builder()
.baseUrl("https://nominatim.openstreetmap.org/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory.invoke())
.client(client)
.build()
.create(AutocompleteAPI::class.java)
suspend fun search(query: String, lon: Double, lat: Double) =
service.search(query, "json", "1", "1", 50, "by")
.apply {
if (query.split(" ").any { it.isDigitsOnly() }) {
sortBy { it.address?.houseNumber ?: "0" }
} else {
sortBy {
(it.lon?.toDouble() ?: 0.0 - lon).pow(2) + (it.lat?.toDouble() ?: 0.0
- lat).pow(2)
}
}
}
.filter { it.address?.road != null }
.map { it.convert() }
}

当您深入okhttp3的Response.java类时,您将看到

public Response build() {
if (request == null) throw new IllegalStateException("request == null");
if (protocol == null) throw new IllegalStateException("protocol == null");
if (code < 0) throw new IllegalStateException("code < 0: " + code);
if (message == null) throw new IllegalStateException("message == null");
return new Response(this);
}

因此,当从Interceptor返回自定义响应时,您也必须传递这些响应。所以。

return Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(999)
.message(msg)
.body(ResponseBody.create(null, "$e")).build()

我已经创建了一个处理Interceptor异常的要点,您可以查看LoggingInterceptor

创建响应(Response.Builder()(时,应提供请求。

try {
it.proceed(it.request())
} catch (e: Exception) {
e.printStackTrace()
Response.Builder().request(it.request()).code(500).build()
}

相关内容

  • 没有找到相关文章

最新更新