I have success response and error response class. When error I get 200 status code. My question is to get the error json object to error kotlin class. How can I handle different types of response in FAN library. I have used JSONDeserializer in Java.
Success Response:
{
"success": true,
"terms": "https://currencylayer.com/terms",
"privacy": "https://currencylayer.com/privacy",
"timestamp": 1430401802,
"source": "USD",
"quotes": {
"USDAED": 3.672982,
"USDAFN": 57.8936,
"USDALL": 126.1652,
"USDAMD": 475.306,
"USDANG": 1.78952,
"USDAOA": 109.216875,
"USDARS": 8.901966,
"USDAUD": 1.269072,
"USDAWG": 1.792375,
"USDAZN": 1.04945,
"USDBAM": 1.757305,
[...]
}
}
Error Response:
{
"success": false,
"error": {
"code": 104,
"info": "Your monthly usage limit has been reached. Please upgrade your subscription plan."
}
}
My Code:
fun getCurrencies(): Disposable {
return Rx2AndroidNetworking.get(GET_CURRENCY)
.addQueryParameter(ACCESS_KEY, ACCESS_VALUE)
.build()
.getObjectObservable(Currency::class.java)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ currency ->
run {
if (currency.success) {
_currency.postValue(currency)
}else {
// get error response
}
}
}, { err -> err.printStackTrace() })
}