Get error returned by the server with Volley

2

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();
    }
}
    
asked by Jorge Gonzalez 16.10.2017 в 03:50
source

2 answers

0

The solution I found here: Link

Thank you all.

    
answered by 17.10.2017 / 02:52
source
2

if you are getting a 404 error when making the request through Volley or it is the url to which you will make the request which is not correct:

 getString(R.string.base_url),

or are the parameters sent:

getString(R.string.payment_methods_url),
getString(R.string.public_key),

in this case the error is that you do not have a public key defined or it is incorrect:

"cause": [
    {
        "code": "1001",
        "description": "public_key not found"
    }
]

This is a more specific description of the error codes:

  

400 BAD_REQUEST

  • 1000 Credentials required.
  • 1001% co_of% not found.
  

401 UNAUTHORIZED

  • Without authorization.
  

404 NOT_FOUND

  • No resource is found ( public_key , url or payment_methods_url ).
answered by 16.10.2017 в 16:23