I need slightly help for using rxjava with retrofi...
# rx
a
I need slightly help for using rxjava with retrofit I have a post request, the status code response could be 200 or 400+ depending on what I have sent, what I need is get the json as a "RegisterResponse" Object as we do on .subscreibe(//here) just to return it as MutableLiveData<RegisterResponse>() object the problem is I received the response as a string how I can changing it to be "RegisterResponse" object
Copy code
val data = MutableLiveData<RegisterResponse>()
 disposables.add(
            ServiceRepo.register(firstName, lastName, mobileno, email, password)
                .subscribeOn(<http://Schedulers.io|Schedulers.io>())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    data.value = it
                }, { throwable ->
        val error: HttpException = throwable as HttpException
                    if (error.code() == 400) {
                        val jObj = error.response().errorBody() as RegisterResponse//the issue here, i can't cast it 
                        data.value = jObj
                    }

                })
        )
i converting it to with gson but i think there is a better solution
a
I actually don't think there is a better solution. I think Retrofit just gives you back errors as strings? but I could be wrong.
❤️ 1
t
the raw response message from an error
retrofit2.Response
would be a
String?
or you might have a non empty
ResponseBody
which you could use source(), string(), etc… to deserialize into your
RegisterResponse
type. but retrofit doesn’t do that for you, you would need to do that yourself. probably a good case for using a SingleTransformer to group that logic into something easy to test and reuse? and since you bring up GSON for JSON with what doesn’t sound like a high level of confidence I recommend you give this a watch and arm yourself with knowledge on the topic: https://vimeo.com/341115830 also you can have other types of errors than just HttpException so you may want to
as?
that first cast. something like a dns fail ends up as a
java.net.UnknownHostException
❤️ 1
b
If you want to see the success code I think you can make your query a Response<RegisterResponse> then extract the code from that then map the RegisterResponse at the end if you have what you want
❤️ 1