How to handle error response with Retrofit 2 using synchronous request?
I need process response that in normal case return pets array and if request has bad parametrs return error json object. How can I process this two situations?
I am trying to use this tutorial but the main problem is mapping normal and error json to objects.
My normal response example:
[ {
"type" : "cat",
"color": "black"
},
{
"type" : "cat",
"color": "white"
} ]
Error response example:
{"error" = "-1", error_description = "Low version"}
What I got:
Call<List<Pet>> call = getApiService().getPet(1);
Response<List<Pet>> response;
List<Pet> result = null;
try {
response = call.execute(); //line with exception "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path"
if(!response.isSuccessful()){
Error error = parseError(response);
Log.d("error message", error.getErrorDescription());
}
if (response.code() == 200) {
result = response.body();
}
} catch (IOException e) {
e.printStackTrace();
}
Retrofit 2 has a different concept of handling "successful" requests than Retrofit 1. In Retrofit 2, all requests that can be executed (sent to the API) and for which you’re receiving a response are seen as "successful". That means, for these requests, the onResponse callback is fired and you need to manually check whether the request is actually successful (status 200-299) or erroneous (status 400-599).
If the request finished successfully, we can use the response object and do whatever we wanted. In case the error actually failed (remember, status 400-599), we want to show the user appropriate information about the issue.
For more details refer this link
After going through a number of solutions. Am posting it for more dynamic use. Hope this will help you guys.
My Error Response
{
"severity": 0,
"errorMessage": "Incorrect Credentials (Login ID or Passowrd)"
}
Below is as usual call method
private void makeLoginCall() {
loginCall = RetrofitSingleton.getAPI().sendLogin(loginjsonObject);
loginCall.enqueue(new Callback<Login>() {
#Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (response != null && response.code() == 200){
//Success handling
}
else if (!response.isSuccessful()){
mServerResponseCode = response.code();
Util.Logd("In catch of login else " + response.message());
/*
* Below line send respnse to Util class which return a specific error string
* this error string is then sent back to main activity(Class responsible for fundtionality)
* */
mServerMessage = Util.parseError(response) ;
mLoginWebMutableData.postValue(null);
loginCall = null;
}
}
#Override
public void onFailure(Call<Login> call, Throwable t) {
Util.Logd("In catch of login " + t.getMessage());
mLoginWebMutableData.postValue(null);
mServerMessage = t.getMessage();
loginCall = null;
}
});
}
Below Is util class to handle parsing
public static String parseError(Response<?> response){
String errorMsg = null;
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
errorMsg = jObjError.getString("errorMessage");
Util.Logd(jObjError.getString("errorMessage"));
return errorMsg ;
} catch (Exception e) {
Util.Logd(e.getMessage());
}
return errorMsg;
}
Below in viewModel observer
private void observeLogin() {
loginViewModel.getmLoginVModelMutableData().observe(this, login -> {
if (loginViewModel.getSerResponseCode() != null) {
if (loginViewModel.getSerResponseCode().equals(Constants.OK)) {
if (login != null) {
//Your logic here
}
}
//getting parsed response message from client to handling class
else {
Util.stopProgress(this);
Snackbar snackbar = Snackbar.make(view, loginViewModel.getmServerVModelMessage(), BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> { });
snackbar.show();
}
} else {
Util.stopProgress(this);
Snackbar snackbar = Snackbar.make(view, "Some Unknown Error occured", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> { });
snackbar.show();
}
});
}
I think you should create a generic response class (let's say GenericResponse), that is extended by a specific response class (let's say PetResponse). In the first one, you include generic attributes (error, error_description), and in the latter, you put your specific response data (List<Pet>).
In your case, I would go with something like this:
class GenericResponse {
int error;
String error_description;
}
class PetResponse extends GenericResponse {
List<Pet> data;
}
So, your successful response body should look like this:
{
"data": [ {
"type" : "cat",
"color": "black"
},
{
"type" : "cat",
"color": "white"
} ]
}
And your error response body should look like this:
{ "error" = "-1", error_description = "Low version"}
Finally, your response object that is returned from the API call should be:
Response<PetResponse> response;
Wrap all your calls in retrofit2.Response like so:
#POST("user/login")
suspend fun login(#Body form: Login): Response<LoginResponse>
A very simple retrofit 2 error handler:
fun <T> retrofitErrorHandler(res: Response<T>): T {
if (res.isSuccessful) {
return res.body()!!
} else {
val errMsg = res.errorBody()?.string()?.let {
JSONObject(it).getString("error") // or whatever your message is
} ?: run {
res.code().toString()
}
throw Exception(errMsg)
}
}
Then use them like:
try {
createdReport = retrofitErrorHandler(api...)
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
Related
I'm implementing a simple login to an endpoints using Retrofit2. Things work fine when the user credentials are correct but break when I try to enter a non valid data.
I'm trying to handle the errors when the user is not found but I can't find a way to do that.
The error response looks like:
{
"0": [
"erreur",
"statut"
],
"erreur": "Erreur, connexion echoue.",
"statut": "KO"
}
This response has status 200 despite being an error.
The app is crashing with NPE in the LoginRepository where I'm trying to save user's data to SharedPreferences because the error result is not handled so the app threat any response as Successful.
The sample provides a Result class which doesn't seem to work for my use case because the response is always successful:
public class Result<T> {
// hide the private constructor to limit subclass types (Success, Error)
private Result() {
}
#Override
public String toString() {
if (this instanceof Result.Success) {
Result.Success success = (Result.Success) this;
return "Success[data=" + success.getData().toString() + "]";
} else if (this instanceof Result.Error) {
Result.Error error = (Result.Error) this;
return "Error[exception=" + error.getError().toString() + "]";
}
return "";
}
// Success sub-class
public final static class Success<T> extends Result {
private T data;
public Success(T data) {
this.data = data;
}
public T getData() {
return this.data;
}
}
// Error sub-class
public final static class Error extends Result {
private Exception error;
public Error(Exception error) {
this.error = error;
}
public Exception getError() {
return this.error;
}
}
}
And here is how I'm handling the login in the LoginRepository:
public Result<LoggedInUser> login(String username, String password) {
// handle login
Result<LoggedInUser> result = dataSource.login(username, password);
if (result instanceof Result.Success) {
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
}
return result;
}
Note: I don't have access to the server. I use Gson as converter
The login activity sample I used can be found here
UPDATE:
Login successful with valid credentials:
Check this answer it will help you.
#POST("end_path")
Call<ResponseBody> LoginCall(
#Field("email") String user_id,
#Part("paassword") String language
);
Call<ResponseBody> call = Constant.service.LoginCall(
"email", "pass");
call.enqueue(new retrofit2.Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String responseData = response.body().string();
JSONObject object = new JSONObject(responseData);
if(object.getString("statut").equalsIgnoreCase("success")){
LoggedInUser successData = new
Gson().fromJson(responseData, LoggedInUser.class);
}else{
showToast("Email password incorrect");//or show you want
this message.
}
} catch (IOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
showToast("something_went_wrong");
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
im doing a test using retrofit and a mock api, i have already a recyclerview with a list of products and the detail of the product while clicking on the row, but in some products the josn is a error handling example :
http://mocklab.io/product/1/
{
"success": true,
"metadata": {
"sku": "2",
"name": "Huawei P20 Pro",
"max_saving_percentage": 30,
"price": 13996,
"special_price": 7990,
"brand": "Huawei",
"rating": {
"average": 4,
"ratings_total": 265
},
but lets say in my case product "3" give me the error json :
http:mocklab.io/product/3/
{
"success": false,
"messages": {
"error": {
"reason": "PRODUCT_NOT_FOUND",
"message": "Product not found!"
}
}
}
the pojos are already done with jsonschema2pojo, so the question is if i click in the 3 row in the recyclerview my app crash because and i get null object reference, but what i want is to hadle the error they ask that is : HTTP 200 - Success false , sorry if i didnt explain myself the best way, and thank you!
call:
GetPhoneDataService service = RetrofitInstance.getRetrofitInstance().create(GetPhoneDataService.class);
Call<APIReponse> call = service.getAllPhones(1);
call.enqueue(new Callback<APIReponse>() {
#Override
public void onResponse(Call<APIReponse> call, Response<APIReponse> response) {
if (response.isSuccessful() && response.body() != null) {
for (Result result : response.body().getMdata().getResults()) {
phoneList.add(result);
}
adapter.setOnClickListener(new PhonesAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
String sku = phoneList.get(position).getSku();
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("sku", sku);
startActivity(intent);
}
});
adapter.notifyDataSetChanged();
} else {
try {
String error = response.errorBody().string();
if (error != null) {
Log.e("Error : ", error);
}
//show error to the user
} catch (Exception e) {
e.printStackTrace();
}
In Retrofit you can handle errors response like this. Inside onResponse callback:
if (response.isSuccessful()
&& response.body() != null) {
//Call was successful and body has data
} else {
try {
String error = response.errorBody().string();
if(error != null)
//show error to the user
} catch (Exception e) {
e.printStackTrace();
}
}
But if your error is thrown here:
public void onItemClick(int position) {
String sku = phonesList.get(position).getSku();
Log.e("ffff", "----- " + sku);
Intent intent = new Intent(MainActivity.this,DetailsActivity.class);
intent.putExtra("sku", sku);
startActivity(intent);
}
then you have problems with not designing your code very well. I don't know context of your code more precisely context when you call generatePhonesList method but, if you need to call it more then once, then consider not setting adapter and recyclerview inside that method. For that you have something called: notifyDataSetChanged()
I am using TMDb API and it returns two different HttpException errorBody type.
One is a proper ApiStatus model which contains status code and message in json.
Other is an error array in json.
So I need to check these two types and convert them to proper Error models.(ApiStatus and Error pojos)
Here is what I'm trying to do to handle two cases:
public abstract class DisposableSingleObserverWrapper<T> extends DisposableSingleObserver<T> {
public abstract void onSuccess(T t);
public abstract void onFail(Throwable e);
#Override
public void onError(#NonNull Throwable e) {
if (e instanceof HttpException) {
ResponseBody body = ((HttpException) e).response().errorBody();
try {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http:/fakeapi/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//New status code handling in tmdb api
Converter<ResponseBody, ApiStatus> errorConverter2 = retrofit.responseBodyConverter(ApiStatus.class, new Annotation[0]);
ApiStatus apiStatus = errorConverter2.convert(body);
if (apiStatus != null && !TextUtils.isEmpty(apiStatus.getStatusMessage())) {
onFail(new Throwable(apiStatus.getStatusMessage()));
}
else {
//Old case of handling status codes in tmdb api
Converter<ResponseBody, Error> errorConverter = retrofit.responseBodyConverter(Error.class, new Annotation[0]);
Error error = errorConverter.convert(body);
if (error != null & error.getErrorList()!=null && error.getErrorList().size()>0) {
onFail(new Throwable(error.getErrorList().get(0)));
}
else{
onFail(new Throwable(((HttpException) e).response().code() + " - " + ((HttpException) e).response().message()));
}
}
} catch (IOException ioException) {
onFail(new Throwable(((HttpException) e).response().code() + " - " + ((HttpException) e).response().message()));
}
} else {
onFail(e);
}
}
public class Error {
#SerializedName("errors")
public ArrayList<String> errorList;
public ArrayList<String> getErrorList() {
return errorList;
}
}
}
This piece of work doesn't work as intended because ResponseBody is consumed in first trial of converting and in second case it has no data. My question is how can I clone ResponseBody or what kind of approach should I follow to deal with this situation?
This question already has answers here:
Get response status code using Retrofit 2.0 and RxJava
(3 answers)
Closed 5 years ago.
I used Retrofit with RxJava like this:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(HttpURL.BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
and when the request error, such as password is wrong, the status code is 400, and the error msg will int the errorBoby to get me just like {code: 1000, message: "password is wrong"}.
However, the gons GsonConverterFactory will not fromJson in respone.getErrorBody , so I change my code just like this
Call<Result<User>> call = ApiManger.get().getLoginApi().login1(login);
call.enqueue(new Callback<Result<User>>() {
#Override
public void onResponse(Call<Result<User>> call, Response<Result<User>> response) {
if (response.code() == 0) {
mLoginView.onLoginSuccess(response.body().getData());
} else {
try {
Result result = new Gson().fromJson(response.errorBody().string(),Result.class);
ToastUtils.showShort(result.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onFailure(Call<Result<User>> call, Throwable t) {
}
});
so it can not be used with Rxjava, how can I change it?
This can be done using Rx and here is how:
mSubscription.add(mDataManager.login(username, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<User>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
if (NetworkUtil.isHttpStatusCode(e, 400) || NetworkUtil.isHttpStatusCode(e, 400)) {
ResponseBody body = ((HttpException) e).response().errorBody();
try {
getMvpView().onError(body.string());
} catch (IOException e1) {
Timber.e(e1.getMessage());
} finally {
if (body != null) {
body.close();
}
}
}
}
#Override
public void onNext(User user) {
//TODO Handle onNext
}
}));
}
NetworkUtil
public class NetworkUtil {
/**
* Returns true if the Throwable is an instance of RetrofitError with an
* http status code equals to the given one.
*/
public static boolean isHttpStatusCode(Throwable throwable, int statusCode) {
return throwable instanceof HttpException
&& ((HttpException) throwable).code() == statusCode;
}
}
You need to serialise the error body string first
try something like this in your onNext():
if (!response.isSuccessful()) {
JSONObject errorBody = new JSONObject(response.errorBody().string());
String message = errorBody.getString("message");
}
Its usually a better idea to accept the response in a standard format -
Class Response{
int code;
String message;
Data data; //////now data is the actual data that u need
/////getter setters
}
Now add an api method like this -
#GET("api_name")
Observable<Response> getResponse(Params);
now call retrofit.getResponse(params) and you will get the observable, subscribe to that observable and check its value in onNext and implement your logic. So in your case(password error) the data would be null, but you will have code and message.
I'm trying to parse the following JSON structure using Retrofit on android.
{
"payload": [
{
"name": "Rice",
"brands": [
{
"name": "Dawat",
"subProducts": [
{
"id": 1,
"name": "Basmati Long Grain",
"creditDays": 20,
"currency": "$",
"willDeliver": false,
"minPrice": 250,
"maxPrice": 400,
"sku": "1Kg",
"uom": ""
}
]
}
]
}
],
"messages": []
}
I have made models using http://www.jsonschema2pojo.org/. The keys I'm particularly are payload-->name, brands-->name and subProducts-->name. Below is what I've tried so far. Can anyone please help? I can't parse this JSON Structure using retrofit
productDetails.enqueue(new Callback<GetProductDetailsByPhoneNumber>() {
#Override
public void onResponse(Call<GetProductDetailsByPhoneNumber> call, Response<GetProductDetailsByPhoneNumber> response) {
Toast.makeText(getApplicationContext(), "Response mil gaya", Toast.LENGTH_LONG);
List<Payload> subProducts = new ArrayList<Payload>(response.body().payload);
}
#Override
public void onFailure(Call<GetProductDetailsByPhoneNumber> call, Throwable t) {
}
});
Interface:
#GET("wholesaler/getProductDetailsByPhoneNumber")
Call<GetProductDetailsByPhoneNumber> getProducts(#Query("phoneNumber") String number);
getDService()
public API getDService(){
/**
* The Retrofit class generates an implementation of the API interface.
*/
if(service == null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(API.class);
}
return service;
}
Payload.java
public class Payload {
public String name;
public List<Brand> brands;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Brand> getBrands() {
return brands;
}
public void setBrands(List<Brand> brands) {
this.brands = brands;
}
}
Try using this, as you are not providing your "Payload"o bject
productDetails.enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if(response.body()!=null{
JsonObject jsonObject=response.body();
if(response.code() == 200){
if(jsonObject.has("payload"){
JsonArray dataArray = jsonObject.getAsJsonArray(HAS_DATA);
if (dataArray.size() > 0) {
Toast.makeText(getApplicationContext(), "Response Called", Toast.LENGTH_LONG);
//your further code
}
}
}
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
//logic for if response fails
}
});
Use this code in your onResponse:
if(response.code()==HttpsURLConnection.HTTP_OK) { //HttpOk is the response code for 200
if (response.body() != null) {
if (response.body().payload!= null) {
//data is there in an array of type payload
//save all the data there
//like you want the name, you can get that by response.body().payload.name and you will get "rice"
//similarly if you want the name which is in subproducts array use : response.body().payload.get(0).brands.get(0).subProducts.get(0).name and you
// will get "Basmati Long Grain"
}
}
}
The code will help you deserialise all the data from the JSON and you can store that wherever you want. Plus I will recommend you to keep a check on other response codes as well(such as 400 for bad request, 500 for internal server error etc). See here, you have your payload in an array, and there was only one element in it, that is why I have used payload.get(0). In case of multiple elements in the array you need to use a loop and then fetch the values, same goes for your brands and subProduct array.
You are trying to get an object PayLoad, and you have
{
"payload": [
{
"name"
this means it doesn't start with a parent, so you need to save the answer in a List like List and letter in the iteration you can use Response.get(position) <-- and this one going to be your payload number position, I hope this can help you