我正在尝试从服务器端获取400,401或500的HTTP状态代码。我尝试了下面的代码,但邮件是null
。
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error fetched/////", "" + error.toString());
// listener.onError(error.toString());
String json = null;
NetworkResponse response = error.networkResponse;
int errcode = error.networkResponse.statusCode;
Log.e("errcode","errcode"+error.networkResponse.toString());
Log.e("HttpStatus", String.valueOf(errcode));
if(HttpStatus.SC_SWITCHING_PROTOCOLS==error.networkResponse.statusCode){
Log.e("HttpStatus","HttpStatus");
}
if (response != null && response.data != null) {
switch (response.statusCode) {
case 400:
json = new String(response.data);
json = trimMessage(json, "message");
listener.onError(error.toString());
Log.e("listenerpost", "listenerpost" + error.getMessage());
break;
case 401:
json = new String(response.data);
json = trimMessage(json, "message");
listener.onError(error.toString());
Log.e("listener1", "listener" + error.networkResponse.toString());
break;
case 500:
json = new String(response.data);
json = trimMessage(json, "message");
listener.onError(error.toString());
Log.e("listener500", "listener500" + error.networkResponse.toString());
}
//Additional cases
}
}
})
请帮我解决这个问题。
答案 0 :(得分:0)
覆盖此方法:
@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
// Here is what you need!
response.statuscode;
}
答案 1 :(得分:0)
问题在于你正试图获得
error.getMessage()
虽然对于你正在尝试的错误是空的,因为BasicNetwork会这样做:
else if (statusCode >= 400 && statusCode <= 499) {
// Don't retry other client errors.
throw new ClientError(networkResponse);
} else if (statusCode >= 500 && statusCode <= 599) {
if (request.shouldRetryServerErrors()) {
attemptRetryOnException("server",
request, new ServerError(networkResponse));
}
基本上错误只是一个Exception类,并且没有设置ClientError和ServerError的消息。
来自服务器的错误信息在
中error.networkResponse
以及获取它的方式应该从服务器获取消息
json = new String(response.data);