am414
08/15/2019, 9:06 PMval 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
}
})
)
alexsullivan114
08/15/2019, 9:32 PMtrevjones
08/15/2019, 10:21 PMretrofit2.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
blakelee
08/16/2019, 8:30 PM