首页 > 解决方案 > 尝试向服务器执行 POST 时获取 java.util.NoSuchElementException。来自服务器的答案是 204/success,但仍然引发异常

问题描述

我对我的服务器进行 POST。结果为 204 / 成功代码,结果正文为空。java.util.NoSuchElementException 在服务器的结果之后立即引发。我的猜测是因为响应不包含正文(Void),但我很不确定

我的界面

    @POST("register/")
    fun register(@Body phone: PhoneBody): Single<Void>

手机机身 data class PhoneBody(val phone: String)

我的查询

  disposable += registrationRepository.register(phone)
            .addSchedulers()
            .doOnSubscribe { viewState.showLoading() }
            .doAfterTerminate { viewState.hideLoading() }
            .subscribe({it ->
                Log.d("PAPDEB", "$it Personal Acc Presenter")

            }, this::handleError)

它永远不会进入订阅 lambda,它会直接进入 this::handle,即

        if (t is ApiException) {
            viewState.showError(t.message)
        } else if (t is IOException) {
            viewState.showError("No internet connection")
        }

        if (BuildConfig.DEBUG) {
            t.printStackTrace()
        }
    }
D/OkHttp: --> POST https://.../register/
D/OkHttp: Content-Type: application/json
D/OkHttp: Content-Length: 24
D/OkHttp: {"phone":"+79090000000"}
D/OkHttp: --> END POST (24-byte body)
D/OkHttp: <-- 204 https://.../register/ (386ms)
D/OkHttp: server: nginx/1.13.12
D/OkHttp: date: Tue, 25 Jun 2019 20:52:49 GMT
D/OkHttp: vary: Accept, Accept-Language, Cookie, Origin
D/OkHttp: allow: POST, OPTIONS
D/OkHttp: x-frame-options: SAMEORIGIN
D/OkHttp: content-language: ru
D/OkHttp: strict-transport-security: max-age=31536000
D/OkHttp: <-- END HTTP

W/System.err: java.util.NoSuchElementException
W/System.err:     at io.reactivex.internal.operators.observable.ObservableSingleSingle$SingleElementObserver.onComplete(ObservableSingleSingle.java:111)
W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onComplete(BodyObservable.java:66)
W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:48)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:12246)
W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:12246)
W/System.err:     at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
W/System.err:     at io.reactivex.Single.subscribe(Single.java:3575)
W/System.err:     at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err:     at java.lang.Thread.run(Thread.java:764)

我将非常感谢听到任何关于什么是错误的以及如何到达 lambda 的想法

标签: androidretrofit2

解决方案


正如@CommonsWare 在对问题的评论中所说,问题是我使用了 Single(从 ctrl+c ctrl+V 的逻辑 -> 更改返回模型)我将其更改为 Completable,现在它可以工作了!


推荐阅读