【问题标题】:E/AndroidRuntime: FATAL EXCEPTION: main and Coroutine, Retrofit2E/AndroidRuntime: FATAL EXCEPTION: main and Coroutine, Retrofit2
【发布时间】:2019-05-19 05:39:34
【问题描述】:

我刚开始学习 kotlin,我的第一个应用程序使用 Retrofit2 和 Coroutine,但是有问题。我有一些错误,但是通过协程,堆栈跟踪的信息非常差。也许有人会发现错误或知道热点以使堆栈跟踪信息更丰富。

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

活动:

 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:

E/AndroidRuntime: 致命异常: main 进程:com.example.daniellachacz.currencyconverter,PID:10924 改造2.HttpException:HTTP 400 在 com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory$BodyCallAdapter$adapt$2.onResponse(CoroutineCallAdapterFactory.kt:104) 在改造 2.OkHttpCall$1.onResponse(OkHttpCall.java:123) 在 okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) 在 okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 在 java.lang.Thread.run(Thread.java:818)

【问题讨论】:

    标签: android kotlin retrofit2 kotlin-coroutines coroutine


    【解决方案1】:

    您收到服务器错误400 Bad Request 响应。此状态码表示由于语法无效,服务器无法理解请求。尝试调试或放置一些日志,看看你真正发送到服务器的内容。我认为调试的好地方是Interceptor接口的实现。

    【讨论】:

    • 是的。你有权利。在 addQueryParameter 我有 ''api'' 而不是 ''apikey''。
    【解决方案2】:

    正如@Sergey 所说,这是一个服务器错误。

    在我的情况下,它无法被 API 捕获,服务器写道:An internal server error occurred.(代码 500)而不是 JSON。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 2020-03-03
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        相关资源
        最近更新 更多