LoginSignup
0
0

More than 5 years have passed since last update.

Kotlin, Retrofit2にて200や201が返ってきてるのにonFailureに入る

Last updated at Posted at 2017-11-14

はじめに

retrofit2でPOSTを送ることはよくあると思うのですが、リスポンスコードとして200や201が返ってきてるのに、onResponseではなく,onFailureに入っちゃうというエラーに遭遇したときの対処法です。
kotlin要素は薄いです。

interfaceでリクエストの返り値をCall<Void>にする

これだけです。

これだけだと寂しいので具体例

もしJsonObjectをPostでAPIサーバーに送りたいってなったらinterfaceは、

APIClient.kt
@Headers("Accept: application/json",
            "Content-Type: application/json")
    @POST("/api/sample")
    fun create_hoge(@Body body: JsonObject): Call<Void>

てな感じに書いたらいけます。

Kotlinでは?

本題です。kotlinのときは,Call<Unit>でいけるのか?というとそういうわけではなく、kotlinのときもCall<Void>です。
UnitとVoidで扱いが違うというか用途はほぼ同じだと思うんですが、Javaの遺産を使うときにはVoidを意識的に使う必要があるみたいです。

参考

https://github.com/square/retrofit/issues/1446
https://stackoverflow.com/questions/22654932/what-is-the-purpose-of-unit-returning-in-functions

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0