简体   繁体   English

E / AndroidRuntime:致命例外:main和协程,Retrofit2

[英]E/AndroidRuntime: FATAL EXCEPTION: main and Coroutine, Retrofit2

I just started learning kotlin and my first app use Retrofit2 and Coroutine, but there is problem. 我刚开始学习kotlin,并且我的第一个应用程序使用Retrofit2和Coroutine,但是有问题。 I have some error, but by Coroutine the stack trace is very poor in informations. 我有一些错误,但是通过协程,堆栈跟踪的信息非常少。 Maybe someone will find a mistake or knows hot to make the stack trace more informative. 也许有人会发现一个错误或知道热点,以使堆栈跟踪更有意义。

ApiService: ApiService:

const val API_KEY = "Bcae2032bb596c73b10bdab625c037da"

interface CurrencyApi {

//https://api.currencystack.io/currency?base=USD&target=EUR&apikey=Bcae2032bb596c73b10bdab625c037da

@GET("currency")
fun getCurrentCurrency(
    @Query("base") base: String,
    @Query("target") target: String
): Deferred<Currency>

companion object {
    operator fun invoke(): CurrencyApi {
        val requestInterceptor = Interceptor { chain ->

            val url = chain.request()
                .url()
                .newBuilder()
                .addQueryParameter("key", API_KEY)
                .build()
            val request = chain.request()
                .newBuilder()
                .url(url)
                .build()

            return@Interceptor chain.proceed(request)
        }

        val okHttpClient = OkHttpClient.Builder()
            .addInterceptor(requestInterceptor)
            .build()

        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl("https://api.currencystack.io/")
            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(CurrencyApi::class.java)
    }
}

Activity: 活动:

 class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    val model = ViewModelProviders.of(this).get(MainViewModel::class.java)

    val apiService = CurrencyApi()

    GlobalScope.launch(Dispatchers.Main) {
        val currency = apiService.getCurrentCurrency("PLN", "EUR").await()
        return@launch try {
            text_view_test.text = currency.toString()
        } catch (e: Exception) {

        }
    }

Logcat: logcat的:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.daniellachacz.currencyconverter, PID: 10924 retrofit2.HttpException: HTTP 400 at com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory$BodyCallAdapter$adapt$2.onResponse(CoroutineCallAdapterFactory.kt:104) at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:123) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) E / AndroidRuntime:致命例外:主进程:com.example.daniellachacz.currencyconverter,PID:10924 retrofit2.HttpException:HTTP 400 at com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory $ BodyCallAdapter $ adapt $ 2.outResponFactory( .kt:104),位于Retrofit2.OkHttpCall $ 1.onResponse(OkHttpCall.java:123),位于okhttp3.RealCall $ AsyncCall.execute(RealCall.java:153),位于okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)的java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)位于java.lang.Thread.run(Thread.java:818)

You receive server error 400 Bad Request response. 您收到服务器错误400 Bad Request响应。 This status code indicates that the server could not understand the request due to invalid syntax. 此状态代码表示服务器由于语法无效而无法理解该请求。 Try to debug or put some logs and see what you are really sending to the server. 尝试调试或放置一些日志,看看您真正发送到服务器的内容。 I think the good place to debug is in the implementation of Interceptor interface. 我认为调试的好地方是Interceptor接口的实现。

As @Sergey said, it is a server error. 正如@Sergey所说,这是服务器错误。

In my case it couldn't be caught by an API and the server wrote: An internal server error occurred. 就我而言,它无法被API捕获,服务器写道: An internal server error occurred. (code 500) instead of a JSON. (代码500)而不是JSON。

@GET(currency)替换为@GET(currency.json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM