【问题标题】:Error java.lang.illegalstateexception request == null错误 java.lang.illegalstateexception 请求 == null
【发布时间】:2020-04-30 16:09:51
【问题描述】:

谁能告诉我可能是什么错误? 有一段时间它工作得很好。而现在,它出现了,然后又消失了。当我开始在自动完成中输入地址时出现错误 我将不胜感激!

开机时设备出现如下错误

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() }
}

【问题讨论】:

  • 不要发布错误日志的图片!

标签: android kotlin httprequest httpresponse okhttp


【解决方案1】:

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

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

【讨论】:

  • 添加了协议。出现消息空错误。然后添加 .message ("")。 java.lang.illegalargumentexception 拦截器出现错误。代码catch (e: Exception) { e.printStackTrace() Response.Builder() .request(it.request()) .protocol(Protocol.HTTP_1_1) .message("") .code(500) .build()
【解决方案2】:

当你深入 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()

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

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多