Retrofit2返回null kotlin中的单位为204否内容响应

我正在使用Retrofit 2.0.2使用以下界面将数据发布到服务器:

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

并从服务器收到成功的回应。

 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) 

但我得到exception下面,似乎改造通过null单位onNextNext。 这是预期的行为?

 05-09 21:20:22.496 23117 23117 W System.err: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method bdbkb, parameter result 05-09 21:20:22.496 23117 23117 W System.err: at com.myandroid.notification.dialog.c$aa(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 egbonNext(SafeSubscriber.java:139) 05-09 21:20:22.496 23117 23117 W System.err: at edag$2$1.onNext(OnSubscribeRedo.java:249) 05-09 21:20:22.496 23117 23117 W System.err: at edap$a.call(OperatorObserveOn.java:215) 05-09 21:20:22.496 23117 23117 W System.err: at eabb$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) 

谢谢。

传递给onNext的实例是响应的(解析的)正文。 由于响应没有正文,因此为null 。 因此, null正在通过。

你应该可以使用

 fun postData(/*args*/): Observable 

解决这个问题的正确方法是使用Completable像这样:

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

那么你可以这样订阅:

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

使用Retrofit 2.2.0,RxJava 2.1.0就像我的魅力一样。