I am making an application in which I consume an external service and for this, I am using the library Volley from Google. I'm trying to process the error with the structure:
{
"message": "invalid public_key",
"error": "not_found",
"status": 404,
"cause": [
{
"code": "1001",
"description": "public_key not found"
}
]
}
but Volley directly calls the method onErrorResponse (VolleyError error) returning a generic error, in this case 404. How should I proceed to obtain this structure and obtain the data I need?
Service outline code:
mProgress = UIComponents.showProgressDialog(this);
Request<?> requestCreditCard = RequestFactory.getCreditCard(this,
getString(R.string.base_url),
getString(R.string.payment_methods_url),
getString(R.string.public_key),
new OkInterface() {
@Override
public void onOkResponse(JSONArray response) throws JSONException {
loadCreditCards(response);
}
},
new ErrorInterface() {
@Override
public void onShowErrorResponse() {
Toast.makeText(getBaseContext(), R.string.without_results, Toast.LENGTH_SHORT).show();
}
@Override
public void doErrorHandler() {
UIComponents.hideProgressDialog(mProgress);
}
});
RequestNetworkCache.getmQueue().add(requestCreditCard);
public class BaseRequestApi extends JsonArrayRequest{
public BaseRequestApi(Context context, String url, OkInterface okInterface,
ErrorInterface errorInterface) {
super(Method.GET, url, null, new BaseResponseApi(context,errorInterface, okInterface), new BaseErrorApi(context,errorInterface));
configRequest(context);
}
private void configRequest(Object cancelTag) {
this.setTag(cancelTag);
this.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
}
@Override
public void onErrorResponse(VolleyError error) {
errorDelegate.onShowErrorResponse();
if(error instanceof TimeoutError)
Toast.makeText(mContext, R.string.time_out_error, Toast.LENGTH_LONG).show();
if(error instanceof NetworkError)
Toast.makeText(mContext, R.string.network_error, Toast.LENGTH_LONG).show();
if(error instanceof NoConnectionError)
Toast.makeText(mContext, R.string.no_connection_error, Toast.LENGTH_LONG).show();
if(error instanceof ServerError)
Toast.makeText(mContext, R.string.server_error, Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(JSONArray response) {
if( response.length() > 0 ) {
try {
okDelegate.onOkResponse(response);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(mContext, mContext.getString(R.string.unexpected_error), Toast.LENGTH_SHORT).show();
errorDelegate.doErrorHandler();
}
}else {
errorDelegate.onShowErrorResponse();
errorDelegate.doErrorHandler();
}
}