I have a simple query like
@FormUrlEncoded @POST("v1/user/login") //your login function in your api Call<LoginResponce> login(@Field("identity") String identity, @Field("password") String password);
which returns either LoginResponceobject if the http code is 200
{"token":"itwbwKay7iUIOgT-GqnYeS_IXdjJi","user_id":17}
Or a Json error, describes the exact error if something went wrong
{"status":4,"description":"user provided token expired"}
How can I handle the error status in response?
I tried this, but it does not see JSON in the raw text (does not work). And it does not seem to be a good solution.
mCallLoginResponse.enqueue(new Callback<LoginResponce>() { @Override public void onResponse(Response<LoginResponce> response, Retrofit retrofit) { if (response.isSuccess()) { registerWithToken(response.body().getToken()); } else { //some error in responce Gson gson = new GsonBuilder().create(); ApiError mApiError = gson.fromJson(response.raw().body().toString(), ApiError.class); //Exception here - no JSON in String //todo error handling } }
source share