我无法正确执行DELETE操作

时间:2019-09-12 17:13:56

标签: android rest android-studio kotlin retrofit2

我正在修复android应用程序中的错误。 通过通过应用发送对某个喜欢的事件ID的DELETE请求, 它正在通过应用程序向api发送正确的事件ID请求,但无法删除请求的事件,并且在请求和响应链接中也显示了不同的事件ID。

这是logcat消息的屏幕截图的链接-> https://drive.google.com/file/d/1dSB65GFNvkLOWopbvMUbDGwlO7S-Ql1P/view?usp=sharing

我从服务器端检查,服务器端没有错误,因此该错误必须在应用程序中的某个位置。

------------------ FavoriteEventApi ----------------------------


import io.reactivex.Completable
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.Path
import retrofit2.http.DELETE

interface FavoriteEventApi {
    @GET("user-favourite-events?include=event")
    fun getFavorites(): Single<List<FavoriteEvent>>

    @POST("user-favourite-events")
    fun addFavorite(
        @Body favorite: FavoriteEvent,
        @Header("Content-Type") header: String = "application/vnd.api+json"
    ): Single<FavoriteEvent>

    @DELETE("user-favourite-events/{favoriteEventId}")
    fun removeFavorite(
        @Path("favoriteEventId") eventId: Long,
        @Header("Content-Type") header: String = "application/vnd.api+json"
    ): Completable
}


------------------ FavoriteEventsViewModel --------------------------- < / p>



import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.auth.AuthHolder
import org.fossasia.openevent.general.common.SingleLiveEvent
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.event.Event
import org.fossasia.openevent.general.event.EventId
import org.fossasia.openevent.general.event.EventService
import timber.log.Timber

class FavoriteEventsViewModel(
    private val eventService: EventService,
    private val resource: Resource,
    private val authHolder: AuthHolder
) : ViewModel() {

    private val compositeDisposable = CompositeDisposable()

    private val mutableProgress = MutableLiveData<Boolean>()
    val progress: LiveData<Boolean> = mutableProgress
    private val mutableMessage = SingleLiveEvent<String>()
    val message: SingleLiveEvent<String> = mutableMessage
    private val mutableEvents = MutableLiveData<List<Event>>()
    val events: LiveData<List<Event>> = mutableEvents

    fun isLoggedIn() = authHolder.isLoggedIn()

    fun loadFavoriteEvents() {
        compositeDisposable +=
            eventService.getFavoriteEvents()
                .withDefaultSchedulers()
                .subscribe({
                    mutableEvents.value = it
                    mutableProgress.value = false
                }, {
                    Timber.e(it, "Error fetching favorite events")
                    mutableMessage.value = resource.getString(R.string.fetch_favorite_events_error_message)
                })
    }

    fun setFavorite(event: Event, favorite: Boolean) {
        if (favorite) {
            addFavorite(event)
        } else {
            removeFavorite(event)
        }
    }

    private fun addFavorite(event: Event) {
        val favoriteEvent = FavoriteEvent(authHolder.getId(), EventId(event.id))
        compositeDisposable += eventService.addFavorite(favoriteEvent, event)
        .withDefaultSchedulers()
        .subscribe({
            mutableMessage.value = resource.getString(R.string.add_event_to_shortlist_message)
        }, {
            mutableMessage.value = resource.getString(R.string.out_bad_try_again)
            Timber.d(it, "Fail on adding like for event ID ${event.id}")
        })
    }

    private fun removeFavorite(event: Event) {
        val favoriteEventId = event.favoriteEventId ?: return

        val favoriteEvent = FavoriteEvent(favoriteEventId, EventId(event.id))
        compositeDisposable += eventService.removeFavorite(favoriteEvent, event)
            .withDefaultSchedulers()
            .subscribe({
                mutableMessage.value = resource.getString(R.string.remove_event_from_shortlist_message)
            }, {
                mutableMessage.value = resource.getString(R.string.out_bad_try_again)
                Timber.d(it, "Fail on removing like for event ID ${event.id}")
            })
    }

    override fun onCleared() {
        super.onCleared()
        compositeDisposable.clear()
    }
}



期望的输出:我们已从您的候选清单中删除了该事件 当前输出:无法像事件ID一样删除 这是LOGCAT消息:-

2019-09-12 22:26:00.889 5168-6415/com.eventyay.attendee D/OkHttp: --> DELETE https://open-event-api-dev.herokuapp.com/v1/user-favourite-events/22
2019-09-12 22:26:00.889 5168-6415/com.eventyay.attendee D/OkHttp: Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NjgzMDM2MjgsIm5iZiI6MTU2ODMwMzYyOCwianRpIjoiMmM1NWU2ZWEtZGM5NC00ZWIxLTg3ZGUtYmQ5NzAyNDU2OWVlIiwiZXhwIjoxNTY4MzkwMDI4LCJpZGVudGl0eSI6OTYsImZyZXNoIjp0cnVlLCJ0eXBlIjoiYWNjZXNzIiwiY3NyZiI6ImU2MWQwNmRlLTkwNDYtNGU3OC04NzhkLTZhMjQ3NjdhZTBiMCJ9.SDVCc7Mp_s4IANicTQsJfeIbGqwMKlTi0nLWN2jf0pk
2019-09-12 22:26:00.889 5168-6415/com.eventyay.attendee D/OkHttp: --> END DELETE
2019-09-12 22:26:00.892 1614-3899/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 3597316 , only wrote 3597120
2019-09-12 22:26:00.905 5168-5168/com.eventyay.attendee W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2019-09-12 22:26:00.919 5168-5168/com.eventyay.attendee I/chatty: uid=10085(com.eventyay.attendee) identical 12 lines
2019-09-12 22:26:00.920 5168-5168/com.eventyay.attendee W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2019-09-12 22:26:01.275 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@eeafb54)
2019-09-12 22:26:01.275 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@eeafb54)
2019-09-12 22:26:02.276 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@998cafd)
2019-09-12 22:26:02.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@998cafd)
2019-09-12 22:26:03.276 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@95b3f2)
2019-09-12 22:26:03.277 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@95b3f2)
2019-09-12 22:26:04.117 1614-1614/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 3904770 , only wrote 3751920
2019-09-12 22:26:04.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@bbd8a43)
2019-09-12 22:26:04.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@bbd8a43)
2019-09-12 22:26:05.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@2a678c0)
2019-09-12 22:26:05.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@2a678c0)
2019-09-12 22:26:06.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@e2c19f9)
2019-09-12 22:26:06.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@e2c19f9)
2019-09-12 22:26:07.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@8471d3e)
2019-09-12 22:26:07.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@8471d3e)
2019-09-12 22:26:08.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@d24879f)
2019-09-12 22:26:08.279 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@d24879f)
2019-09-12 22:26:09.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@dda60ec)
2019-09-12 22:26:09.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@dda60ec)
2019-09-12 22:26:10.278 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@20b5cb5)
2019-09-12 22:26:10.279 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@20b5cb5)
2019-09-12 22:26:11.278 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@e13cf4a)
2019-09-12 22:26:11.279 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@e13cf4a)
2019-09-12 22:26:12.277 1891-1993/? I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@4d3febb)
2019-09-12 22:26:12.278 1891-1905/? I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@4d3febb)
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: <-- 404 NOT FOUND https://open-event-api-dev.herokuapp.com/v1/user-favourite-events/22 (12228ms)
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Connection: keep-alive
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Content-Type: application/vnd.api+json
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Content-Length: 146
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Access-Control-Allow-Origin: *
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Server: Werkzeug/0.15.4 Python/3.6.5
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Date: Thu, 12 Sep 2019 16:56:13 GMT
2019-09-12 22:26:13.118 5168-6415/com.eventyay.attendee D/OkHttp: Via: 1.1 vegur
2019-09-12 22:26:13.119 5168-6415/com.eventyay.attendee D/OkHttp: {"errors": [{"status": 404, "source": {"pointer": ""}, "title": "Object not found", "detail": "Object Not Found"}], "jsonapi": {"version": "1.0"}}
2019-09-12 22:26:13.119 5168-6415/com.eventyay.attendee D/OkHttp: <-- END HTTP (146-byte body)
2019-09-12 22:26:13.127 5168-5168/com.eventyay.attendee D/FavoriteEventsViewModel$removeFavorite: Fail on removing like for event ID 35
    retrofit2.adapter.rxjava2.HttpException: HTTP 404 NOT FOUND
        at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
        at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
        at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:47)
        at io.reactivex.Observable.subscribe(Observable.java:12267)
        at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
        at io.reactivex.Observable.subscribe(Observable.java:12267)
        at io.reactivex.internal.operators.observable.ObservableIgnoreElementsCompletable.subscribeActual(ObservableIgnoreElementsCompletable.java:31)
        at io.reactivex.Completable.subscribe(Completable.java:2309)
        at io.reactivex.internal.operators.completable.CompletableAndThenCompletable.subscribeActual(CompletableAndThenCompletable.java:35)
        at io.reactivex.Completable.subscribe(Completable.java:2309)
        at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)


1 个答案:

答案 0 :(得分:0)

2019-09-12 22:26:13.119 5168-6415 / com.eventyay.attendee D / OkHttp:{“错误”:[{“状态”:404,“源”:{“指针”:“” },“ title”:“ 未找到对象”,“ detail”:“未找到对象”}},“ jsonapi”:{“ version”:“ 1.0”}}

响应正文失败,我认为您尝试删除的favoriteEventId在服务器端未找到,因此您收到此错误