我正在制作一个我使用外部服务的应用程序,为此,我使用的是Google的Volley库。我试图用结构处理错误:
{
"message": "invalid public_key",
"error": "not_found",
"status": 404,
"cause": [
{
"code": "1001",
"description": "public_key not found"
}
]
}
但Volley直接调用onErrorResponse方法(VolleyError错误)返回一般错误,在本例中为404.如何继续获取该结构并获取我需要的数据?
服务架构代码:
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.D`enter code here`EFAULT_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();
}
}