Retrofit2 return null Unit in kotlin for 204 No Content response

风流意气都作罢 提交于 2020-01-23 08:37:47

问题


I'm using Retrofit 2.0.2 to post data to server using the interface below:

@POST("/path/to/post")
fun postData(/*args*/): Observable<Unit>

And received success response from server.

05-09 21:20:21.770 23117 23224 D OkHttp  : --> POST https://myserver.com/api/notifications/123/markRead http/1.1
05-09 21:20:21.770 23117 23224 D OkHttp  : Content-Length: 0
05-09 21:20:21.770 23117 23224 D OkHttp  : User-Agent: MyUserAgent
05-09 21:20:21.770 23117 23224 D OkHttp  : Authorization: Bearer xxxxx
05-09 21:20:21.770 23117 23224 D OkHttp  : --> END POST (0-byte body)
05-09 21:20:22.473 23117 23224 D OkHttp  : <-- 204 No Content https://myserver.com/api/notifications/123/markRead (701ms)

But i got exception below, seems retrofit pass null Unit to onNext. Is this expected behaviour?

05-09 21:20:22.496 23117 23117 W System.err: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method b.d.b.k.b, parameter result
05-09 21:20:22.496 23117 23117 W System.err:    at com.myandroid.notification.dialog.c$a.a(NotificationListPresenter.kt)
05-09 21:20:22.496 23117 23117 W System.err:    at com.myandroid.notification.dialog.c$a.onNext(NotificationListPresenter.kt:57)
05-09 21:20:22.496 23117 23117 W System.err:    at e.g.b.onNext(SafeSubscriber.java:139)
05-09 21:20:22.496 23117 23117 W System.err:    at e.d.a.g$2$1.onNext(OnSubscribeRedo.java:249)
05-09 21:20:22.496 23117 23117 W System.err:    at e.d.a.p$a.call(OperatorObserveOn.java:215)
05-09 21:20:22.496 23117 23117 W System.err:    at e.a.b.b$b.run(LooperScheduler.java:107)
05-09 21:20:22.496 23117 23117 W System.err:    at android.os.Handler.handleCallback(Handler.java:730)
05-09 21:20:22.504 23117 23117 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 21:20:22.504 23117 23117 W System.err:    at android.os.Looper.loop(Looper.java:137)
05-09 21:20:22.504 23117 23117 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:5103)
05-09 21:20:22.504 23117 23117 W System.err:    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 21:20:22.504 23117 23117 W System.err:    at java.lang.reflect.Method.invoke(Method.java:525)
05-09 21:20:22.504 23117 23117 W System.err:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-09 21:20:22.504 23117 23117 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-09 21:20:22.504 23117 23117 W System.err:    at dalvik.system.NativeStart.main(Native Method)

Thank you.


回答1:


The instance passed into onNext is the (parsed) body of the response. Since the response has no body, it is null. Therefore, null is being passed.

You should be able to use

fun postData(/*args*/): Observable<Unit?>



回答2:


The right way of solving this problem is to use Completable like this:

@POST("/path/to/post")
fun postData(/*args*/): Completable 

Then you can subscribe like this:

service.postData(/*args*/)
    .subscribe({ view.dataPostedSuccesfully() }, { handleError(it) })

Work like a charm for me with Retrofit 2.2.0, RxJava 2.1.0.



来源:https://stackoverflow.com/questions/37116364/retrofit2-return-null-unit-in-kotlin-for-204-no-content-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!