My API returns this when I use wrong login information (using postman):
{
"error": {
"statusCode": 401,
"name": "Error",
"message": "login failed",
"code": "LOGIN_FAILED",
"stack": "Error: login failedn at ...path..."
}
}
I am using this method to get the response message:
private void handleResponse(retrofit2.Response<Response> response) {
if (response.isSuccessful()) {
emailField.setText(null);
passwordField.setText(null);
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
try {
showSnackBarMessage(response.errorBody().string());
} catch (Exception e) {
showSnackBarMessage(e.getMessage());
}
}
}
And the output (what snackbar shows) is the same as postman returns.
handleresponse
parameter retrofit2.Response<Response> response
consists of retrofit2 Response
, and my own <Response>
class which looks like this:
public class Response {
@SerializedName("message")
@Expose
private String message;
public String getMessage() {
return message;
}
}
How can I get only message
to show in snackbar
?
I have tried the following code, but I get only No value for message
.
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}