0

An API we use to login a user recently changed where if they need to take an action, a string is sent in the body instead of a JSON (which gets converted to an object). Therefore, I need to be able to handle this.

I changed my observable response to handle ANY like so:

    @POST("api/v1/user/login")
    fun postLogin(@Body body: LoginBody): Observable<Response<Any>>

but when I go to subscribe on this observable I am getting an exception that JSON has not been fully consumed.

postLogin(LoginBody(username, password))
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe({
                                    if (it.isSuccessful) {

                                        val res = LoginRemote.parseResponse(it as Response<LoginRemote>)
                                        if (res.result is Result.Ok) {
                                            res.result.data.uid?.let { sessionData?.uid = it }
                                            res.result.data.username?.let { sessionData?.username = it }
                                            res.result.data.email?.let { sessionData?.email = it }
                                            res.result.data.phoneType?.let { sessionData?.phoneType = it }
                                            res.result.data.phone?.let { sessionData?.phone = it }
                                            res.result.data.verified?.let { sessionData?.verified = it }
                                            res.result.data.role?.let { sessionData?.role = it }
                                            res.result.data.gender?.let { sessionData?.gender = it }
                                            res.result.data.countryOfResidence?.let { sessionData?.countryOfResidence = it }
                                        }
                                        acceptTermsResponse.value = res
                                    } else {
                                        acceptTermsResponse.value = LoginResponse(listOf(LoginResponse.ErrorType.Generic()))
                                    }
                                }, {
                                    ERRORS HERE --> acceptTermsResponse.value = LoginResponse(listOf(LoginResponse.ErrorType.Generic()))
                                })

Is there a way to subscribe to a two types of response data?

I found this post and attempted the solution of adding the Scalar Converter Factory to my retrofit but that didn't solve the problem.

BlondeSwan
  • 772
  • 5
  • 26
  • why don't you just leave your response type as String and then handle it later ? you can try to cast to one object, if it fails cast to the other – a_local_nobody May 10 '21 at 19:41
  • 1
    Add ToString converter factory in your retrofit. https://stackoverflow.com/questions/34170409/retrofit-with-string-response – Muhammad Ahmed May 10 '21 at 20:14

0 Answers0