【发布时间】:2014-03-19 01:00:46
【问题描述】:
我正在为我的 Android 应用程序使用 Volley 从我的服务器获取数据。它运行良好,除非处理来自我的服务器的错误。当出现错误时,我的服务器会发送此响应:
{
"status": 400,
"message": "Errors (2): A name is required- Julien is already used. Not creating."
}
我的目标是获取消息,然后将其显示在 Toast 中。我遵循了一些示例来了解如何执行此操作,但它不起作用。
有我的错误监听器:
public void onErrorResponse(VolleyError error) {
int statusCode = error.networkResponse.statusCode;
NetworkResponse response = error.networkResponse;
Log.d("testerror",""+statusCode+" "+response.data);
// Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button.
// For AuthFailure, you can re login with user credentials.
// For ClientError, 400 & 401, Errors happening on client side when sending api request.
// In this case you can check how client is forming the api and debug accordingly.
// For ServerError 5xx, you can do retry or handle accordingly.
if( error instanceof NetworkError) {
} else if( error instanceof ClientError) {
} else if( error instanceof ServerError) {
} else if( error instanceof AuthFailureError) {
} else if( error instanceof ParseError) {
} else if( error instanceof NoConnectionError) {
} else if( error instanceof TimeoutError) {
}
showProgress(false);
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
还有我的调试器的结果: testerror:400 [B@430b8d60
编辑:此外,我的 error.getMessage() 为空。
所以我不明白为什么我的变量 response.data 不是我服务器的响应。
如果有人知道我如何从服务器获取消息,那就太好了。
谢谢,
【问题讨论】: